Step 5
Stopping movement when a key is released
When you press the arrow keys to move around, the character keeps on moving. Ideally the movement would stop when the arrow key is released, so we'll fix that. We'll add some true/false variables so that we can check which keys are being pressed, and then we'll add blocks to detect when keys are released so that we can stop the player's movement.
First, create 4 new true/false property variables for "moving left", "moving right", "moving up", and "moving down". Grab "set true/false i" from the Properties section of Operators and drag it out into the workspace. Click on "i" and select "New variable..."
Name this variable "moving left", and then repeat this 3 more times for "moving right", "moving up", and "moving down".
In each key press block, make sure the appropriate variable is set to true, e.g. "When player presses left arrow, set true/false left to true" (you can find the "true" block in Operators).
Then duplicate the 4 "When player presses key" blocks, and change the duplicated blocks to "When player releases key". Remove the "set rotation" blocks from these duplicate events, since we don't need to rotate when a key is released. We also want to change these new variables to false when the keys are released, e.g. "When player releases left arrow, set moving left to false".
Now so that we don't accidentally stop the player from moving if they're already moving in the other direction, we'll add blocks to check if the player is moving in the opposite direction, and if they aren't then we'll make sure their velocity gets set to 0. Grab these blocks:
"if" (from Control Flow)
"true/false moving right" (from Variables)
"=" (from Operators)
"false" (starts as "true" from Operators)
Connect those blocks together so they read "if true/false moving right = flase", and then drag "set velocity x 0" inside the "if" block. These blocks should go inside "When player releases left arrow".
Then make 3 more duplicates and put them in the other key release blocks. Don't forget to make sure that up and down use "set velocity y 0" instead of "set velocity x 0".
Play your game again to see the movement stop when you release the arrow keys.