Skip to content

Please update your browser

Your browser version is not supported by this site. CS First may not work properly. Learn more

3. Muovere lo sprite a sinistra e a destra


In this screencast, you’ll program key press events to move the sprite left and right.

Remember, the left and right values on the stage are x values. You can use a “change x by” block to move the sprite left and right.

Try this by clicking on a “change x by” block, and you’ll see that the sprite moves right.

A value of 10 moves the sprite right, and a value of -10 moves it left. So, drag out another “move x by” block and change the value to -10.

Now, you need to tell the computer when to run this code.

To do this, you’ll need an event. Go to events, and select a “when key pressed” event for both blocks.

Make the left movement the left arrow, and the right movement the right arrow.

Now, test your code to make sure it works!... Perfect!

The sprite should now move left and right with the arrow keys, and jump when the up arrow is pressed. You may notice that your 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. You solved this problem in the racing game, and you can use a similar solution here.

Use a “repeat until” block to continue running the “change x by” block until the arrow key is no longer being pressed. Then, select a “not” block from “operators” and a “key pressed” block from sensing.

Change the value of the sensing block to “right key pressed.” This block stack now reads: “Repeat moving right until the right arrow key is no longer being pressed.”

Once you complete this stack, try it out!

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

arrow_backward Indietro
Avanti arrow_forward
  1. Crea due situazioni su tastiera.
  2. Aggiungi un blocco "Cambia x di" per far muovere lo sprite sia a sinistra sia a destra.
  3. Aggiungi un blocco "Ripeti fino a quando" per ciascuna situazione per far muovere agevolmente il tuo sprite.
  4. Per ciascun blocco "Ripeti fino a quando" utilizza una condizione che faccia muovere lo sprite fino a quando non viene premuto il tasto