Re:processing project for UNI












-2















i m pretty sure i changed it to follow the rules so i m asking again.
The problem is that i want to shoot a bullet thats initial direction will follow the where the mouse INITIALLY was compared to location of character on mouseclick. but my code now on mouseclick will draw another bullet, but all bullets drawn will travel in a direction following an angle respective to current character location on mouseclick(which is NOT what i want)



this is an example of what it looks like



int bulletTotal = 5;
float bulletX = new float [bulletTotal];
float bulletY = new float [bulletTotal];
int shots;
float bulletSpeed;
float angle = new float [bulletTotal];
int charaStartX, charaStartY;
int charaWid, charaHght;
int sx, sy;
boolean isUp, isDown, isLeft, isRight;

void setup()
{
size(1500,900);
charaStartX = width/2;
charaStartY = height/2;
charaWid = charaHght = 75;
sx = sy = 4;
for(int j =0; j < bulletTotal; j++)
{
bulletX[j] = charaStartX;
bulletY[j] = charaStartY;
}
bulletSpeed = 5;
isUp = false;
isDown = false;
isLeft = false;
isRight = false;
}

void draw()
{
ellipse(charaStartX, charaStartY, charaWid, charaHght);
for(int j = 0; j < bulletTotal; j++)
{
rect(bulletX[j], bulletY[j], 25, 25);
movement();
bulletX[j] += bulletSpeed * cos(angle[j]);
bulletY[j] += bulletSpeed * sin(angle[j]);
}
}

void movement()
{
if(isUp == true)
{
charaStartY = charaStartY - sy;
}else if(isDown == true)
{
charaStartY = charaStartY + sy;
}
if(isLeft == true)
{
charaStartX = charaStartX - sx;
}else if (isRight == true)
{
charaStartX = charaStartX + sx;
}
}

void mousePressed()
{
if (shots<bulletTotal)
{
bulletX[shots] = charaStartX;
bulletY[shots] = charaStartY;
shots = shots+1;
}
for(int j=0; j<bulletTotal; j++)
{
angle[j] = atan2(mouseY - charaStartY, mouseX - charaStartX);
}
}
void keyPressed()
{
if(key == 'w'||key == 'W')
{
isUp = true;
}
if(key == 's'||key == 'S')
{
isDown = true;
}
if(key == 'a'||key == 'A')
{
isLeft = true;
}
if(key =='d'||key == 'D')
{
isRight = true;
}

}

void keyReleased()
{
if(key == 'w'||key == 'W')
{
isUp = false;
}
if(key == 's'||key == 'S')
{
isDown = false;
}
if(key == 'a'||key == 'A')
{
isLeft = false;
}
if(key =='d'||key == 'D')
{
isRight = false;
}
}









share|improve this question

























  • What programming language is this? Also, you may want to post complete code which can be tried (or, at least an example code showing the problem which can be tried).

    – prasad_
    Nov 21 '18 at 5:57













  • ok. i ll add the draw another bullet thing. as for programming language i m not too sure as this is a entry level course for year 1 students where we use an app called processing.

    – Tan Yu Hau Sean
    Nov 21 '18 at 6:38
















-2















i m pretty sure i changed it to follow the rules so i m asking again.
The problem is that i want to shoot a bullet thats initial direction will follow the where the mouse INITIALLY was compared to location of character on mouseclick. but my code now on mouseclick will draw another bullet, but all bullets drawn will travel in a direction following an angle respective to current character location on mouseclick(which is NOT what i want)



this is an example of what it looks like



int bulletTotal = 5;
float bulletX = new float [bulletTotal];
float bulletY = new float [bulletTotal];
int shots;
float bulletSpeed;
float angle = new float [bulletTotal];
int charaStartX, charaStartY;
int charaWid, charaHght;
int sx, sy;
boolean isUp, isDown, isLeft, isRight;

void setup()
{
size(1500,900);
charaStartX = width/2;
charaStartY = height/2;
charaWid = charaHght = 75;
sx = sy = 4;
for(int j =0; j < bulletTotal; j++)
{
bulletX[j] = charaStartX;
bulletY[j] = charaStartY;
}
bulletSpeed = 5;
isUp = false;
isDown = false;
isLeft = false;
isRight = false;
}

void draw()
{
ellipse(charaStartX, charaStartY, charaWid, charaHght);
for(int j = 0; j < bulletTotal; j++)
{
rect(bulletX[j], bulletY[j], 25, 25);
movement();
bulletX[j] += bulletSpeed * cos(angle[j]);
bulletY[j] += bulletSpeed * sin(angle[j]);
}
}

void movement()
{
if(isUp == true)
{
charaStartY = charaStartY - sy;
}else if(isDown == true)
{
charaStartY = charaStartY + sy;
}
if(isLeft == true)
{
charaStartX = charaStartX - sx;
}else if (isRight == true)
{
charaStartX = charaStartX + sx;
}
}

void mousePressed()
{
if (shots<bulletTotal)
{
bulletX[shots] = charaStartX;
bulletY[shots] = charaStartY;
shots = shots+1;
}
for(int j=0; j<bulletTotal; j++)
{
angle[j] = atan2(mouseY - charaStartY, mouseX - charaStartX);
}
}
void keyPressed()
{
if(key == 'w'||key == 'W')
{
isUp = true;
}
if(key == 's'||key == 'S')
{
isDown = true;
}
if(key == 'a'||key == 'A')
{
isLeft = true;
}
if(key =='d'||key == 'D')
{
isRight = true;
}

}

void keyReleased()
{
if(key == 'w'||key == 'W')
{
isUp = false;
}
if(key == 's'||key == 'S')
{
isDown = false;
}
if(key == 'a'||key == 'A')
{
isLeft = false;
}
if(key =='d'||key == 'D')
{
isRight = false;
}
}









share|improve this question

























  • What programming language is this? Also, you may want to post complete code which can be tried (or, at least an example code showing the problem which can be tried).

    – prasad_
    Nov 21 '18 at 5:57













  • ok. i ll add the draw another bullet thing. as for programming language i m not too sure as this is a entry level course for year 1 students where we use an app called processing.

    – Tan Yu Hau Sean
    Nov 21 '18 at 6:38














-2












-2








-2








i m pretty sure i changed it to follow the rules so i m asking again.
The problem is that i want to shoot a bullet thats initial direction will follow the where the mouse INITIALLY was compared to location of character on mouseclick. but my code now on mouseclick will draw another bullet, but all bullets drawn will travel in a direction following an angle respective to current character location on mouseclick(which is NOT what i want)



this is an example of what it looks like



int bulletTotal = 5;
float bulletX = new float [bulletTotal];
float bulletY = new float [bulletTotal];
int shots;
float bulletSpeed;
float angle = new float [bulletTotal];
int charaStartX, charaStartY;
int charaWid, charaHght;
int sx, sy;
boolean isUp, isDown, isLeft, isRight;

void setup()
{
size(1500,900);
charaStartX = width/2;
charaStartY = height/2;
charaWid = charaHght = 75;
sx = sy = 4;
for(int j =0; j < bulletTotal; j++)
{
bulletX[j] = charaStartX;
bulletY[j] = charaStartY;
}
bulletSpeed = 5;
isUp = false;
isDown = false;
isLeft = false;
isRight = false;
}

void draw()
{
ellipse(charaStartX, charaStartY, charaWid, charaHght);
for(int j = 0; j < bulletTotal; j++)
{
rect(bulletX[j], bulletY[j], 25, 25);
movement();
bulletX[j] += bulletSpeed * cos(angle[j]);
bulletY[j] += bulletSpeed * sin(angle[j]);
}
}

void movement()
{
if(isUp == true)
{
charaStartY = charaStartY - sy;
}else if(isDown == true)
{
charaStartY = charaStartY + sy;
}
if(isLeft == true)
{
charaStartX = charaStartX - sx;
}else if (isRight == true)
{
charaStartX = charaStartX + sx;
}
}

void mousePressed()
{
if (shots<bulletTotal)
{
bulletX[shots] = charaStartX;
bulletY[shots] = charaStartY;
shots = shots+1;
}
for(int j=0; j<bulletTotal; j++)
{
angle[j] = atan2(mouseY - charaStartY, mouseX - charaStartX);
}
}
void keyPressed()
{
if(key == 'w'||key == 'W')
{
isUp = true;
}
if(key == 's'||key == 'S')
{
isDown = true;
}
if(key == 'a'||key == 'A')
{
isLeft = true;
}
if(key =='d'||key == 'D')
{
isRight = true;
}

}

void keyReleased()
{
if(key == 'w'||key == 'W')
{
isUp = false;
}
if(key == 's'||key == 'S')
{
isDown = false;
}
if(key == 'a'||key == 'A')
{
isLeft = false;
}
if(key =='d'||key == 'D')
{
isRight = false;
}
}









share|improve this question
















i m pretty sure i changed it to follow the rules so i m asking again.
The problem is that i want to shoot a bullet thats initial direction will follow the where the mouse INITIALLY was compared to location of character on mouseclick. but my code now on mouseclick will draw another bullet, but all bullets drawn will travel in a direction following an angle respective to current character location on mouseclick(which is NOT what i want)



this is an example of what it looks like



int bulletTotal = 5;
float bulletX = new float [bulletTotal];
float bulletY = new float [bulletTotal];
int shots;
float bulletSpeed;
float angle = new float [bulletTotal];
int charaStartX, charaStartY;
int charaWid, charaHght;
int sx, sy;
boolean isUp, isDown, isLeft, isRight;

void setup()
{
size(1500,900);
charaStartX = width/2;
charaStartY = height/2;
charaWid = charaHght = 75;
sx = sy = 4;
for(int j =0; j < bulletTotal; j++)
{
bulletX[j] = charaStartX;
bulletY[j] = charaStartY;
}
bulletSpeed = 5;
isUp = false;
isDown = false;
isLeft = false;
isRight = false;
}

void draw()
{
ellipse(charaStartX, charaStartY, charaWid, charaHght);
for(int j = 0; j < bulletTotal; j++)
{
rect(bulletX[j], bulletY[j], 25, 25);
movement();
bulletX[j] += bulletSpeed * cos(angle[j]);
bulletY[j] += bulletSpeed * sin(angle[j]);
}
}

void movement()
{
if(isUp == true)
{
charaStartY = charaStartY - sy;
}else if(isDown == true)
{
charaStartY = charaStartY + sy;
}
if(isLeft == true)
{
charaStartX = charaStartX - sx;
}else if (isRight == true)
{
charaStartX = charaStartX + sx;
}
}

void mousePressed()
{
if (shots<bulletTotal)
{
bulletX[shots] = charaStartX;
bulletY[shots] = charaStartY;
shots = shots+1;
}
for(int j=0; j<bulletTotal; j++)
{
angle[j] = atan2(mouseY - charaStartY, mouseX - charaStartX);
}
}
void keyPressed()
{
if(key == 'w'||key == 'W')
{
isUp = true;
}
if(key == 's'||key == 'S')
{
isDown = true;
}
if(key == 'a'||key == 'A')
{
isLeft = true;
}
if(key =='d'||key == 'D')
{
isRight = true;
}

}

void keyReleased()
{
if(key == 'w'||key == 'W')
{
isUp = false;
}
if(key == 's'||key == 'S')
{
isDown = false;
}
if(key == 'a'||key == 'A')
{
isLeft = false;
}
if(key =='d'||key == 'D')
{
isRight = false;
}
}






processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 6:44







Tan Yu Hau Sean

















asked Nov 21 '18 at 4:34









Tan Yu Hau SeanTan Yu Hau Sean

12




12













  • What programming language is this? Also, you may want to post complete code which can be tried (or, at least an example code showing the problem which can be tried).

    – prasad_
    Nov 21 '18 at 5:57













  • ok. i ll add the draw another bullet thing. as for programming language i m not too sure as this is a entry level course for year 1 students where we use an app called processing.

    – Tan Yu Hau Sean
    Nov 21 '18 at 6:38



















  • What programming language is this? Also, you may want to post complete code which can be tried (or, at least an example code showing the problem which can be tried).

    – prasad_
    Nov 21 '18 at 5:57













  • ok. i ll add the draw another bullet thing. as for programming language i m not too sure as this is a entry level course for year 1 students where we use an app called processing.

    – Tan Yu Hau Sean
    Nov 21 '18 at 6:38

















What programming language is this? Also, you may want to post complete code which can be tried (or, at least an example code showing the problem which can be tried).

– prasad_
Nov 21 '18 at 5:57







What programming language is this? Also, you may want to post complete code which can be tried (or, at least an example code showing the problem which can be tried).

– prasad_
Nov 21 '18 at 5:57















ok. i ll add the draw another bullet thing. as for programming language i m not too sure as this is a entry level course for year 1 students where we use an app called processing.

– Tan Yu Hau Sean
Nov 21 '18 at 6:38





ok. i ll add the draw another bullet thing. as for programming language i m not too sure as this is a entry level course for year 1 students where we use an app called processing.

– Tan Yu Hau Sean
Nov 21 '18 at 6:38












1 Answer
1






active

oldest

votes


















1














The problem in your code is



for(int j=0; j<bulletTotal; j++)
{
angle[j] = atan2(mouseY - charaStartY, mouseX - charaStartX);
}


When you press the mouse, it sets the angle for all bullets. You only want to set it for the bullet you just created. You need to delete the part above and use:



if (shots<bulletTotal)
{
bulletX[shots] = charaStartX;
bulletY[shots] = charaStartY;
angle[shots] = atan2(mouseY - charaStartY, mouseX - charaStartX);
shots = shots+1;
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405311%2freprocessing-project-for-uni%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The problem in your code is



    for(int j=0; j<bulletTotal; j++)
    {
    angle[j] = atan2(mouseY - charaStartY, mouseX - charaStartX);
    }


    When you press the mouse, it sets the angle for all bullets. You only want to set it for the bullet you just created. You need to delete the part above and use:



    if (shots<bulletTotal)
    {
    bulletX[shots] = charaStartX;
    bulletY[shots] = charaStartY;
    angle[shots] = atan2(mouseY - charaStartY, mouseX - charaStartX);
    shots = shots+1;
    }





    share|improve this answer




























      1














      The problem in your code is



      for(int j=0; j<bulletTotal; j++)
      {
      angle[j] = atan2(mouseY - charaStartY, mouseX - charaStartX);
      }


      When you press the mouse, it sets the angle for all bullets. You only want to set it for the bullet you just created. You need to delete the part above and use:



      if (shots<bulletTotal)
      {
      bulletX[shots] = charaStartX;
      bulletY[shots] = charaStartY;
      angle[shots] = atan2(mouseY - charaStartY, mouseX - charaStartX);
      shots = shots+1;
      }





      share|improve this answer


























        1












        1








        1







        The problem in your code is



        for(int j=0; j<bulletTotal; j++)
        {
        angle[j] = atan2(mouseY - charaStartY, mouseX - charaStartX);
        }


        When you press the mouse, it sets the angle for all bullets. You only want to set it for the bullet you just created. You need to delete the part above and use:



        if (shots<bulletTotal)
        {
        bulletX[shots] = charaStartX;
        bulletY[shots] = charaStartY;
        angle[shots] = atan2(mouseY - charaStartY, mouseX - charaStartX);
        shots = shots+1;
        }





        share|improve this answer













        The problem in your code is



        for(int j=0; j<bulletTotal; j++)
        {
        angle[j] = atan2(mouseY - charaStartY, mouseX - charaStartX);
        }


        When you press the mouse, it sets the angle for all bullets. You only want to set it for the bullet you just created. You need to delete the part above and use:



        if (shots<bulletTotal)
        {
        bulletX[shots] = charaStartX;
        bulletY[shots] = charaStartY;
        angle[shots] = atan2(mouseY - charaStartY, mouseX - charaStartX);
        shots = shots+1;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 4 '18 at 20:23









        J.D.J.D.

        69028




        69028






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405311%2freprocessing-project-for-uni%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?