In this video, you’ll program a sprite to jump when the up arrow key is pressed, andfall 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 aretwo values: an x and a y. X values are the horizontal values. A negativex position is on the left side of the stage, and a positive x position is on the rightside of the stage. Y values are the vertical values. A positivey position is on the top of the screen, and a negative y position is on the bottom ofthe screen. To make the sprite move up, drag out a “changey” 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 somethinglarger, like 100 …Whooooaaaa… that moves up too much. Tinker with the value in the "change y" block untilyou 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 positivevalue moved the sprite up, so try a negative value to move it down. This example uses avalue of -50. Click the block to test. Great! The spritecomes 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 blockto 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 touchinga 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 thatall the surfaces that the sprite stands on are black, so use a “touching color” blockfrom 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 menuand add a “not” block. Lastly, add an event to tell the computerwhen 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, movesdown, 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 downwith the “change y by” block 2) Program the sprite to move up when theup arrow is pressed 3) Use an if statement and forever loop tomake the sprite go down when not touching a black platformRemember, if you have a question while programming, ask a neighbor or get the attention of yourCS First Guru.