2. Use Events to Make the Sprite Jump
In this video, you’ll program a sprite to jump when the up arrow key is pressed, and fall when it’s not touching a platform. To move the sprite in different directions, you’ll use the x and y coordinate system. For every position on the stage, there are two values: an x and a y. X values are the horizontal values. A negative x position is on the left side of the stage, and a positive x position is on the right side of the stage. Y values are the vertical values. A positive y position is on the top of the screen, and a negative y position is on the bottom of the screen. To make the sprite move up, drag out a “change y” block. Click the block to test. Great! The sprite moves up.
Moving up by 10 isn’t high enough to reach a platform, so try changing the value to something larger, like 100 … Whooooaaaa… that moves up too much. Tinker with the value in the "change y" block until you get an effect you like. This example uses a value of 50.
To program the sprite to move back down, drag out another “change y” block. A positive value moved the sprite up, so try a negative value to move it down. This example uses a value of -50. Click the block to test. Great! The sprite comes back down. Next, tell the program when to run this code.
Click on events, and add a “when space key pressed” block to the up movement.
This sprite should move up when the up arrow is pressed, so change the value of this block to up arrow. Try it out out now.
Nice! Pressing the up arrow makes the sprite move up.
Next, program the sprite to move down. The sprite should fall if it’s not touching a platform, so program the sprite to make a decision.
If the sprite is not touching the ground or a platform, then it should move down.
Select an “if” block from the control menu, and place it around the “change y” block. This starter project was designed so that all the surfaces that the sprite stands on are black, so use a “touching color” block from the sensing menu as the condition. Now this reads, if touching black, move down, but you want the sprite to move down if it’s not touching black. Go to the operators menu and add a “not” block. Lastly, add an event to tell the computer when to run this code. Go to events, drag out a “when flag clicked” block, and add it to this “if” block. Test this!
Oh no, the sprite moved down, but stopped before it reached the color black.
In this case, the if statement is programmed to run only one time. When the flag is clicked, the computer checks the if statement, sees that the sprite isn’t touching black, moves down, and stops running.
To make the program continually check if the sprite is not touching black, click control, and add a forever loop around the if statement Now test this. Awesome.
If the sprite is falling too fast for your game, change the -50 in the “change y” block to something smaller. This example uses -10.
Now it’s your turn: 1) Program the sprite to move up and down with the “change y by” block 2) Program the sprite to move up when the up arrow is pressed 3) Use an if statement and forever loop to make the sprite go down when not touching a black platform Remember, if you have a question while programming, ask a neighbor or get the attention of your CS First Guru.