How to Make Your Sprite Move in Scratch Using Arrow Keys or WASD

by caydeny in Circuits > Websites

21678 Views, 2 Favorites, 0 Comments

How to Make Your Sprite Move in Scratch Using Arrow Keys or WASD

Screenshot 2023-06-20 202048.jpg

Have you ever wanted to make a moving character in Scratch but coding is too hard? Luckily, I have the solution for you, a guide on how to make your sprite in Scratch move using arrow keys or WASD! This guide will help you to give your character smooth movement and is easy to follow!

Supplies

All you will need is a device that can connect to the internet. Although you do not need a Scratch account, I highly recommend one if you want to be able to save your progress.

Choose Your Sprite

scratch instructable.jpg
scratch.jpg

First, you need pick a sprite that you want to make move. You can pick your sprite by going to the bottom right corner of the screen and pressing the cat icon. This will bring you to a library of characters which you can use.

Create the Variables

scratch 2.jpg
scratch 1.jpg

In order to help the sprite move smoothly, we have to create variables. The variables I made were 'x vel', 'y vel', and 'jump times'. 'x vel' and 'y vel' help the character move left and right, and up and down. 'jump times' helps prevent the player from spamming the jump button and glitching. You can use any name for the variables as long as you know which one is which.

To create your variable, go to the variables section on the left hand side of the screen. Click on the 'Make a Variable' button, and it will bring you to a screen where you can name your variable. Once you have made your three variables, bring out a 'when green flag clicked' block and place it in the workspace. Bring out three 'set (variable) to (0)'. Change the '(variable)' on each block to a different variable that you have chose, like in the image above.

Right Arrow or 'd' Movement

scratch 3.jpg
scratch 4.jpg

Now, you have to create left and right movement. In the first picture, it shows one of the scripts of code needed to make right movement. In the code it says that if right arrow is pressed and 'x vel' is smaller than 20, then change 'x vel' by 2.

In the second picture, it shows the second script needed to make right movement. It says that if right or left arrow is pressed then change x by 'x vel', and if 'x vel' is smaller than 0, change 'x vel' by 1, else change 'x vel' by -1. Repeat until 'x vel' equals 0. This makes the movement smooth by making the character be able to build up momentum, and slow it down using the same type of code. The repeat makes the code work until the character has fully stopped meaning that it won't keep moving to one side.

The reason why I did right or left arrow is because it helps reduce the total amount of code for doing the same thing.


Left Arrow or 'a' Movement

scratch 5.jpg

To make left movement, just copy the first piece of code in Step 3 and change the values to left arrow and put a negative on the '20' and the '2'. Alternatively you can just copy the code in the picture above.

Jumping

scratch 6.jpg
scratch 7.jpg

You now have right and left movement for your character, but now you need to be able to jump. The code for jumping is in the two pictures above.

In the first picture, it has the first piece of code needed to jump. When up arrow is pressed and jump times equals 0, then it will change 'y vel' by 30 and 'jump times' by 1. This piece of code checks that the player has or hasn't already pressed jump and prevents the character from jumping twice and being trapped in the air. The reason it says change 'y vel' by 30 is because that is how much the character jumps by.

In the second picture, it shows the second script of code in order to jump. It says that when up arrow is pressed and 'jump times' is 1, then it will change y by 'y vel', and will change 'y vel' by -3 and change y by 'y vel' and then repeat until 'y vel' equals -30. After that, it resets 'jump times' and 'y vel' to 0. This code is the movement behind the jump. It makes the sprite move up and then down enough to make it land in the exact same y level that it started on.

Test

Now that you have finished your code, test it out by clicking the green flag and using the arrow keys or WASD. If there is any glitches or errors, there is a mistake in the code and you must find it and correct it in order to make the code function correctly.

Once you have done that, you can now use it to make your own 'platformer' game or whatever you want. Have fun!