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. Die Figur von links nach rechts bewegen

Transcript

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 Zurück
Weiter arrow_forward
Anweisungen
  1. Erstellt zwei Tastendruck-Ereignisse.
  2. Fügt zwei Blöcke „ändere x um“ hinzu, um die Figur nach rechts und links zu bewegen.
  3. Fügt zwei Blöcke „wiederhole bis“ hinzu, damit sich die Figur gleichmäßiger bewegt.
  4. Erstellt für jeden Block „wiederhole bis“ eine Bedingung, sodass sich die Figur bewegt, bis die Taste nicht mehr gedrückt wird.