Skip to content
https://storage.googleapis.com/files.cs-first.com/game-design/game-icon%402x.png

3. Move the Sprite Left and Right

Transcript

In this video, you'll program key-press events to move the sprite left and right.

To move the sprite left and right, use change x blocks.

Select motion, and drag out a change x block.

Click the block to test.

Great, the sprite moves to the right.

To move the sprite to the left, drag out another change x block, and change the value to negative ten.

Click the block to test.

Nice.

Next, tell the computer when to run this code.

Click events and select the when key pressed block for both blocks.

Change the drop-down to the left arrow key for the left movement, and the right arrow key for the right movement.

Now test your code.

The sprite now moves left and right with the arrows keys and jumps when the up arrow is pressed.

The sprite isn't moving smoothly when it moves left and right.

If you press and hold the right arrow, the sprite will move to the right and stop for a bit, before moving again.

The sprite should continually move, until the arrow key is no longer being pressed.

Use a repeat until block to fix this.

Drag out a repeat until block from the control menu.

Then select a not block from operators and a key-press block from sensing.

Change the value of the sensing block to right key press.

This block stack now reads, repeat moving until the right arrow key is no longer being pressed.

Try it out.

Awesome, now the sprite moves to the right smoothly.

Apply the same change to the left arrow by duplicating your code and changing the values to match your left arrow.

Great.

Now it's your turn.

Move your sprite left and right with change x by blocks.

Use key press events to tell the computer when to run the change x blocks.

Then, run this code until the key is no longer being pressed with a repeat until block.

Instructions

  1. Create two key-press events.
  2. Add two "change x by" blocks to make the sprite move both left and right.
  3. Add two "repeat until" blocks to make the sprite move more smoothly.
  4. For each "repeat until" block, create a condition that makes the sprite move until the key is not pressed.