We are going to learn how to use the keyboard to move our sprite in different directions. We will learn about coordinates and how to move our sprite on the x and y axis. We will be using the same categories that we have covered previously: events, control and motion.
Step 1. Setting up our scene!
Delete your cat sprite from the sprites pane.
Hint: right click on your sprite in the sprites pane!
Click the sprite button (above) in the sprites pane.
Add any sprite from the Underwater theme.
Click the picture button (above) in the sprites pane and add any backdrop from the Underwater theme.
Step 2. Moving the sprite!
when space key pressed block and change it to the up arrow.
Hint: click the down arrow next to the word space!
change y by 10block.
Test your code by pressing the up key. What happens?
How can we add the other directions?
Next add these three scripts:
The stage is made of x and y coordinates. The x axis runs horizontally (left and right), the y axis runs vertically (up and down)
Add this script to your sprite:
Where will the sprite go when the green flag is clicked?
Test your code by clicking the green flag. What happens?
repeat 10 block to each of your
key pressed scripts.
Hint: you should add 4 more blocks in total.
Your scripts should look like this:
Test your code by pressing the arrow keys. What happens?
Can you remember how to change the speed and the distance that the sprite moves?
Step 3. Challenges!
Well done, you have used the arrow keys on your keyboard to control your sprite!
Ready for some challenges?
Make your sprite draw a line when it moves
Hint: Look in the Pen category!
Make your sprite change costume when it moves