In this screencast, you’ll program key press events to move the sprite left and right.
Remember, the left and right values on thestage are x values. You can use a “change x by” block to move the sprite left and right.
Try this by clicking on a “change x by” block,and you’ll see that the sprite moves right.
A value of 10 moves the sprite right, and a value of -10 moves it left. So, drag outanother “move x by” block and change the value to -10.
Now, you need to tell the computer when to run this code.
To do this, you’ll need an event. Go to events, and select a “when key pressed”event for both blocks.
Make the left movement the left arrow, and the right movement the right arrow.
Now, test your code to make sure it works!... Perfect!
The sprite should now move left and right with the arrow keys, and jump when the uparrow is pressed. You may notice that your sprite isn’t movingsmoothly when it moves left and right. If you press and hold the right arrow, the spritewill move to the right and stop for a bit before moving again. You solved this problemin the racing game, and you can use a similar solution here.
Use a “repeat until” block to continue running the “change x by” block untilthe arrow key is no longer being pressed. Then, select a “not” block from “operators”and a “key pressed” block from sensing.
Change the value of the sensing block to “right key pressed.” This block stack now reads:“Repeat moving right until the right arrow key is no longer being pressed.”
Once you complete this stack, try it out!
Awesome! Apply the same change to the left arrow by duplicating your codeand changing the values to match the left arrow.