Unit 3 Help - Student's Free Play Code

Hi - My student’s program has a sprite shooting arrows. His problem, in his words, “the ‘e’ button can be used multiple times without the arrow hitting something. I can use the code as much as i want but that’s the problem. I want the arrow to go off the screen before the code can be called again. Once the arrow leaves the screen I want the ability to call it again without it being used while the other arrow is still visible.”
Lines 47 through 59 contain the area of question.

What he expects to happen is that the arrow will shoot off the screen before allowing another arrow to be shot. What is happening is it is continuing to loop. He worked on trying to end the loop before it shoots another arrow but he doesn’t know how. I am not able to help him - he is ahead of me : - /
I pasted his code above as well as below here:

@tcreager,

This looks like the fun beginnings to a game. Here’s a suggestion that could help.

Around line 53 in the playerShoot function, they are telling the arrow to shoot every time the e key is pressed. Can you use a nested if statement? So, if the e key is pressed, another conditional checks to see if the playerArrow.x is < 51 or >500?

Then, it would only fire if the e key is pressed and if it is at it’s initial position or off the screen.

Does that make sense?

Check back in if that still isn’t working!

Mike

1 Like

Thank you! I will share this with my student this afternoon and let you know. I am still learning along with my students. I am happy to say I believe I understand your suggestion : - )

Thank you for your help. This is what my student coded with your suggestion:

function playershoot() {
if (keyWentDown(“e”)) {
playerWeapon.setAnimation(“emptybow”);
playerArrow.x = playerStill.x;
playerArrow.y = playerStill.y;
playerArrow.velocityX = 6;
} if (playerArrow.x > 500) {
playerArrow.x = playerStill.x;
playerArrow.y = playerStill.y;
} if (playerArrow.x < 51) {
playerArrow.x = playerStill.x;
playerArrow.y = playerArrow.y;
playerArrow.velocityX = 0;
}
}

This did stop his bow from repeatedly firing arrows until each one is off the screen. Right now he is stuck because he also wants to code another sprite to be the target. Ultimately he wants the arrow to hit the target and stop firing. He doesn’t know where to start with that. Now we are going on break for a week and will be back on 10-25. If you have any suggestions to help him and leave it here, I will get that info to him then. Thank you for your help!

@tcreager,

I’m not sure exactly what he has planned, but I might suggest he create the other sprite, determine how or when that sprite would be in contact with the arrow and then look at some of the following blocks:

[Sprite].isTouching(target)
[Sprite].visible

There is always more than one way to solve a problem, but those blocks could be part of a solution.

Then, if he gets stuck again, you could share his code and we may be able to give some pointers.

Mike