zaro

How do you make a character move left and right in Scratch?

Published in Scratch Movement 4 mins read

To make a character (sprite) move left and right in Scratch, you primarily use event blocks to detect key presses and motion blocks to change the sprite's horizontal position on the stage. This allows for interactive control of your sprite using the keyboard.

Understanding Horizontal Movement in Scratch

In Scratch, the stage is a 2D coordinate system. Horizontal movement is controlled along the X-axis.

  • Moving Right: Increases the sprite's X-coordinate.
  • Moving Left: Decreases the sprite's X-coordinate.

The center of the stage is typically at X: 0, Y: 0. The X-axis ranges from approximately -240 (far left) to 240 (far right).

Step-by-Step Guide to Implementing Left and Right Movement

Follow these steps to program your sprite to move using the arrow keys:

1. Select Your Sprite

First, ensure the sprite you want to move is selected in the Sprites pane below the stage.

2. Program Movement for Right Arrow

  1. Start with an Event Block: From the Events category, drag a when [space] key pressed block into the scripting area.
  2. Change Key: Click the dropdown arrow on the space part of the block and select right arrow.
  3. Add Motion Block: From the Motion category, drag a change x by (10) block and attach it below the when [right arrow] key pressed block. The value 10 determines the speed; higher numbers mean faster movement.

3. Program Movement for Left Arrow

  1. Duplicate or Repeat: You can either duplicate the blocks you just created (right-click on when [right arrow] key pressed and select "Duplicate") or drag new blocks.
  2. Change Key: For the new set of blocks, click the dropdown on the when [right arrow] key pressed block and select left arrow.
  3. Adjust Motion Block: For left movement, you need to decrease the X-coordinate. Change the value in the change x by (10) block to change x by (-10).

4. (Optional but Recommended) Set Initial Position

To ensure your character always starts at a specific spot when the project begins:

  1. From the Events category, drag a when Green Flag clicked block.
  2. From the Motion category, drag a go to x: (0) y: (0) block and attach it below the when Green Flag clicked block. Adjust the X and Y values to your desired starting point.

5. (Optional) Control Sprite Direction and Rotation

If your sprite has a specific front or needs to face the direction of movement without flipping upside down, adjust its direction and rotation style.

Movement Direction Recommended Blocks (Motion Category) Purpose
Move Right point in direction (90) Makes the sprite face right (default direction). Attach before change x by (10).
Move Left point in direction (-90) or point in direction (left) Makes the sprite face left. Attach before change x by (-10).
Rotation Style set rotation style [left-right] Prevents the sprite from flipping vertically when changing direction. Add this under when Green Flag clicked.

You can also set the rotation style directly from the sprite's information panel (the little "i" icon next to the sprite's name in the Sprites pane).

Summary of Essential Blocks

Here's a quick overview of the blocks commonly used for left and right movement:

Action Event Block Motion Block (X-Axis) Optional Direction Control
Initialize Position when Green Flag clicked go to x: (0) y: (0) set rotation style [left-right]
Move Right when [right arrow] key pressed change x by (10) point in direction (90)
Move Left when [left arrow] key pressed change x by (-10) point in direction (-90)

Testing Your Movement

After adding the scripts to your sprite, click the Green Flag located above the stage to start your project. Then, use the left and right arrow keys on your keyboard to test if your character moves as intended. You can adjust the change x by values to make your character move faster or slower.