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. Déplacer le lutin vers la gauche et vers la droite :

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 Retour
Suivant arrow_forward
Instructions
  1. Crée deux évènements clavier.
  2. Ajoute deux blocs "Ajouter à x" pour faire en sorte que le lutin se déplace à gauche comme à droite.
  3. Ajoute deux blocs "Répéter jusqu’à" pour faire en sorte que le lutin se déplace de façon plus fluide.
  4. Pour chaque bloc "Répéter jusqu’à", crée une condition qui fait en sorte que le lutin se déplace jusqu'à ce que la touche ne soit plus appuyée.