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 : - )

1 Like

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!

1 Like

@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

Hi Mike - thanks for your help so far. I apologize because apparently I did not communicate the original coding problem that my student is up against. I was in error when I told you that he was able to use the code to stop his bow from repeatedly firing. He is still experiencing that same problem.

So - what he would like to happen is that the sprite arrow fires, either hits a target (to be coded later) or moves off the screen before the sprite arrow is allowed to repeat being fired despite the user pressing the e key.

What is happening now is that his sprite arrow continues to shoot out while the first arrow is still in motion when the e key is depressed.

Ultimately is there a way to make the “e” key active or inactive until the sprite arrow hits a target or moves off the screen?

1 Like

@tcreager,

In looking at his code, it appears he doesn’t have nested conditional statements, but rather three separate ones meaning that it’s possible for multiple of them to be executed at once.

Here’s a block of code showing nested statements. In this case, both conditions have to be true for the arrow to shoot

Screen Shot 2021-10-25 at 1.05.24 PM

So, the e key has to be pressed AND the arrow has to be either ready to shoot or already off the page.

His code would actually work if that first if block was wrapped around the other two rather than just being by itself and in front of the other two.

Hopefully this helps a little.

Mike

1 Like