Ora

How to Make a Scratch Sprite Move Left and Right

Published in Scratch Programming Movement 5 mins read

To make a Scratch sprite move left and right, you need to use specific coding blocks that adjust its horizontal position (X-axis). This can be achieved through two primary methods: using "when key pressed" events for direct control or using a "forever" loop with "if" statements for continuous, smoother movement.

Understanding X-Axis Movement in Scratch

In Scratch, the Stage is a 2D coordinate system.

  • The X-axis controls horizontal movement (left and right).
  • The Y-axis controls vertical movement (up and down).
  • The center of the Stage is (0, 0).
  • Moving right increases the X value.
  • Moving left decreases the X value.

Method 1: Using "When Key Pressed" Events (Instant Movement)

This is the simplest way to get your sprite moving. Each time a specific key is pressed, the sprite instantly moves a set amount.

Step-by-Step for Moving Right

  1. Start an Event: Drag a when [right arrow] key pressed block from the Events category to the scripting area. This block triggers your code when the right arrow key is pressed.
  2. Change X-position: From the Motion category, drag a change x by [10] block and snap it below the event block.
    • The value 10 means the sprite will move 10 steps to the right. You can adjust this number to change the speed. A higher number means faster movement.

Step-by-Step for Moving Left

  1. Start an Event: Drag another when [left arrow] key pressed block from the Events category.
  2. Change X-position: From the Motion category, drag a change x by [-10] block and snap it below this new event block.
    • The value -10 means the sprite will move 10 steps to the left. Remember, negative values decrease the X-position.

Putting It Together

Your scripts should look like this:

  • For Moving Right:
    when [right arrow] key pressed
    change x by (10)
  • For Moving Left:
    when [left arrow] key pressed
    change x by (-10)

Once you've added these scripts to your sprite, you can press the Green Flag in Scratch. Then, simply use the left and right arrow keys on your keyboard to make your sprite move back and forth!

Method 2: Using a "Forever" Loop with "If" Statements (Continuous Movement)

This method provides smoother and more responsive movement, as the script constantly checks whether a key is being pressed.

Why Use a Forever Loop?

Using a forever loop allows the sprite to move continuously as long as the key is held down, rather than just one step per key press. This makes gameplay feel more natural.

Scripting Continuous Left/Right Movement

  1. Start the Program: Drag a when [green flag clicked] block from the Events category to the scripting area. This block starts your movement script when the green flag is clicked.
  2. Create a Loop: Drag a forever block from the Control category and snap it below the when green flag clicked block. This loop will constantly check for key presses.
  3. Check for Right Arrow: Inside the forever loop, drag an if < > then block from the Control category.
    • Inside the hexagonal space of the if block, drag a key [right arrow] pressed? block from the Sensing category.
    • Inside the if block, add a change x by [5] block from the Motion category.
  4. Check for Left Arrow: Below the first if block (still inside the forever loop), drag another if < > then block.
    • Inside its hexagonal space, drag a key [left arrow] pressed? block from the Sensing category.
    • Inside this if block, add a change x by [-5] block from the Motion category.

Your Complete Script

when [green flag clicked]
forever
    if <key [right arrow] pressed?> then
        change x by (5)
    end
    if <key [left arrow] pressed?> then
        change x by (-5)
    end
end

With this script, press the Green Flag and then use the left and right arrow keys on your keyboard. Holding down an arrow key will now make your sprite glide smoothly across the screen.

Enhancing Sprite Movement

To make your sprite's movement even better, consider these optional additions:

  • Adjusting Speed: Change the number in change x by blocks. Smaller numbers for slower movement, larger for faster.
  • Preventing Rotation: If your sprite is a character, it might rotate when changing direction. To fix this, add a set rotation style [left-right] block (from Motion) to your when green flag clicked script, or to your sprite's initialization.
  • Adding Animation: Switch between different costumes (sprite appearances) using next costume blocks from the Looks category to simulate walking. You can place these within your if blocks.
  • Setting Boundaries: To prevent your sprite from moving off-screen, you can use if on edge, bounce (from Motion) or specific if statements to check the sprite's x position and stop it from going too far.

For more detailed information and to experiment with these blocks, visit the official Scratch website.

Troubleshooting Common Issues

  • Sprite moving too fast or too slow: Adjust the number in the change x by blocks.
  • Sprite flipping upside down: Use the set rotation style [left-right] block once at the beginning of your script.
  • Movement not responding: Double-check that your event blocks (when key pressed or when green flag clicked) are correctly connected and that the keys in the key [] pressed? blocks match the keys you are pressing.