Using code to add movement can help tell your audience about your hero.
In this video, you will program your hero sprite to move when the arrow keys are pressed.
To start, drag out a “change y by” block from the Motion menu.
“Y” is the vertical, or up and down, location of your sprite on the stage.
Use a positive number to move your hero up.
Program your sprite to move up when the up arrow key is pressed. From the Events menu,
drag out a “when key pressed” event and attach it to the “change y by” block.
Select “up arrow” from the drop-down menu.
Test your code.
Great! To make your sprite move more OR less, adjust the values in the blocks.
Now add code that tells the computer to move your hero down when you click the down arrow key!
To do this, duplicate your code
and select the down arrow key from the dropdown menu
and update the value with a negative number.
Make your hero sprite move left and right with a “change x by” block.
“X” is the horizontal, or side to side, location of your sprite on the stage.
Use a positive number to program your sprite to move to the right.
From the Events menu, drag out a “when key pressed” event and attach it to the top of the “change x by” block.
Select “right arrow” from the drop down menu.
Duplicate this code and update the values to program your hero to move left when the left arrow is pressed.
Test your program again.
Awesome! Now your hero moves up, down, and side to side with the arrow keys.
Once you’ve programmed your hero to move, try “Fly Over Buildings” to keep adding to your project.
Then, come back to this page to check out more videos! You can try any video you like.
Make your hero move up and down when arrow keys are pressed.
Program buildings to move across the screen, so your hero appears to fly.
Set the scene for your hero by adding sound to your project.
Help your hero do great things with this spinning move.
Create a game by programming your hero to collect objects.
Add another character to help your hero.
Use the Paint Editor in Scratch to draw your own hero.
Para hacer esta actividad en español, haz clic aquí.
Sign in to save your project and track your progress.