My students and I are confused about the first part of the instructions:
Find the if statement that checks whether the sprite is on the ground, and look at the if statement inside of it that checks whether the user has pressed the “up” arrow key. ( Show me where )
Use the velocityY block to make the frog jump up when the user presses the arrow key.
Use the velocityY block to make the frog stop moving otherwise.
In the answer the frog keeps moving no matter what after the up arrow key is pressed. The directions make it sound like the frog should only be going up when the key is pressed.
Thanks for reaching out! For this you’ll need to use an If/Else block to use both of the “velocityY” blocks. The IF checks to see if (keyWentDown("up")) and the else has the frog.velocityY = 0; so it isn’t moving UNLESS the up arrow key is pressed (making the condition statement = true). This If/Else will be inside of the bigger IF statement that checks the position of the frog (frog.y > 324).
So once the “up” arrow is pressed, the frog.y position changes by -2 (going up, or subtracting from y) and the bigger IF statement (frog.y > 324) is no longer true.
The second part of the question has you create a statement that IF the frog reaches a certain height, the velocity will change to +positive and move the frog back down until the original IF statement is true and the frog.velocityY will be equal to 0.
// if frog is on ground which the frog.y is greater than 324 (in my case)
// jump if user presses up arrow, otherwise(else) stop which means velocityY = 0
if (frog.y > 324) {
if(keyWentDown("up")){
frog.velocityY = -3;
} else {
frog.velocityY = 0;
}
}
// if the frog gets high enough
// send it back down
if (frog.y < 125) {. // 125 in my case, students could have it jump higher
frog.velocityY = 3;
}
With the solution below, the frog does not stop moving even when the up arrow is not being pressed. It only goes back to start once it reaches the top.
// Create the mushroom
var mushroom = createSprite(350, 335);
mushroom.setAnimation(“mushroom”);
mushroom.velocityX = -1;
// Create the frog
var frog = createSprite(175, 325);
frog.setAnimation(“frog”);
function draw() {
background(“skyblue”);
fill(“burlywood”);
noStroke();
rect(0, 360, 400, 40);
// if frog is on ground,
// jump if user presses up arrow, otherwise stop.
if (frog.y > 324) {
if(keyWentDown(“up”)){
frog.velocityY = -3;
} else {
frog.velocityY = 0;
}
}
// if the frog gets high enough
// send it back down
if (frog.y <= 0) {
frog.velocityY = 1;
}
// if the mushroom has gone off the left edge,
// move it to the right edge
if (mushroom.x < -30) {
mushroom.velocityX = 1;
}
if (frog.isTouching(mushroom)) {
frog.setAnimation(“frog2”);
stopSound(“sound://default.mp3”);
}
drawSprites();
}
Yes, that is the the way the code is intended to work. It’s supposed to simulate pushing the “jump” command in a video game. So, when you press the button, the frog “jumps” by moving up to the desired height, then moving back down. I think Brad was referring to the fact that when the frog is on the ground, it doesn’t move unless the user presses the button.
The velocity is set to zero to keep the frog from falling through the ground when it falls back down. Our three different cases in which we might want to change behavior are:
The frog is on the ground AND the user presses the up button -> start the jump by giving the frog an upwards velocity.
The frog is on the ground AND the user is not pressing the up button -> stop the frog to ensure that it doesn’t fall through the ground.
The frog reaches the maximum height -> start the frog’s descent by giving it a downward velocity.
Otherwise, the frog will continue as it is (moving up, moving down, or staying stationary).
Thanks, so many students were asking how to make the frog “stop” as that is in the directions. I will tell them it simply means not jumping after it has jumped.
I think the issue students are having is as follows: although the video suggests that the sprite.velocityX = 2 is nothing more than sprite.x = sprite.x + 2, in reality the velocity command actually starts its own loop or process that operates independent of the draw loop, which is also running. For this reason, one doesn’t need to hold down the “up arrow” key for the frog to continue to move up vertically until it hits the trigger point to come back down. I think this concept should be explained (assuming I have it right myself:thinking:).
Using sprite.velocityX = 2 is the same as putting sprite.x = sprite.x + 2 inside the draw loop in that every time the draw loop runs, it will automatically run sprite.x = sprite.x + sprite.velocityX. The process for the velocity isn’t run separately from the draw loop itself, or else the timings could get out of sync.
I see what you’re saying that the students may interpret that as being that the two lines of code do exactly the same thing wherever they are. This level is designed to correct that misconception. The power behind sprite.velocityX is that it only needs to be called once, and so it can be used in a conditional in this way. Otherwise, you would need to create your own sprite velocity variable.
We’ll look at more ways to correct this misconception when we do our next revision.
After reading this, I’m still a little confused about level 11. It seems as though the frog should move up by -2 when the key is pressed, otherwise he is not moving, but it doesn’t work that way. Can someone explain this to me?
After looking at this again, I see that the frog.y starts at 325, then it goes to 323. So the second time through the loop, frog.y is not greater than 324 so it doesn’t ever set velocityY to 0. That’s why the frog keeps moving after the up button is pressed.
This line of code (frog.velocityY = 0) is actually there to make the frog stop moving down once he hits the ground.
I know this is an old post - but just wanted to comment we had the exact same confusion in my class as the students were trying to get it to “stop” when up was not being pressed down. Not stop when it hits the ground.
" * Make the frog jump up when the user presses the arrow key.
Make the frog stop moving otherwise."
My students and I were pretty stumped. Glad I can bring this info for them tomorrow, but thought I’d just bump this as it’s still a bit befuddling to students!