Skip to content

Please update your browser

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

2. Use Events to Make the Sprite Jump

Transcript

In this video, you'll program a sprite to jump when the up arrow key is pressed and fall when it's not touching a platform.

To move the sprite in different directions, you'll use the X and Y coordinate system.

For every position on the stage, there are two values, an X and a Y.

X values are the horizontal values.

A negative X position is on the left side of the stage, and a positive X position is on the right side of the stage.

Y values are the vertical values.

A positive Y position is on the top of the screen and a negative Y position is on the bottom of the screen.

To make the sprite move up, drag out a change Y block.

Click the block to test, Great, the sprite moves up.

Moving up by 10 isn't high enough to reach a platform, so try changing the value to something larger, like 100.

Wow, that moves up way too much.

Tinker with the value in the change Y block, until you get an effect you like.

This example uses a value of 50.

To bring the sprite to move back down, drag out another change Y block.

A positive value moved the sprite up, so try a negative value to move it down.

This example uses a value of negative 50.

Click the block to test.

Great, the sprite comes back down.

Next, tell the program when to run this code.

Click on events, and add a when space key pressed block to the up movement.

This sprite should move up when the up arrow key is pressed.

So, change the value of this block to up arrow.

Try it out now.

Nice, pressing the upper arrow makes the sprite move up.

Next, program the sprite to move down.

The sprite should fall if its not touching a platform.

So, program the sprite to make a decision.

If the sprite is not touching the ground or a platform, then it should move down.

Select and F block from the control menu and place it around the change Y block.

This starter project was design so that all the surfaces that the sprite stands on are black.

So, use a touching color black from the sensing menu as the condition.

Now, this reads, if touching black move down.

But you want the sprite to move down, if it's not touching black.

Go to operators menu, and add a not block.

Lastly, add an event to tell the computer when to run this code.

Go to events, drag out a one flag clicked block, and add it to this if block.

Test this.

Oh no, the sprite move down, but stopped before it reached the color black.

In this case, the if statement is programed to run only one time.

When the flag is clicked, the computer checks the if statement, sees that the sprite isn't touching black, moves down and stops running.

To make the program continually check if the sprite is not touching black, click control, and add a forever loop around the if statement.

Now, test this.

Awesome.

If the sprite is falling too fast for your game, change the negative 50 in the change Y block, to something smaller.

This example uses negative 10.

Now it's your turn.

Program the sprite to move up and down with the change Y by blocks.

Program the sprite to move up, when the up arrow key is pressed.

Then, use an if statement and a forever loop, to make the sprite go down when not touching a black platform.

arrow_backward Back
Next arrow_forward
Instructions
  1. Program your player sprite to jump when the up arrow key is pressed.
  2. Create a condition that says if the sprite is not touching a platform, then it should move down.
  3. Make the player sprite fall when it is not touching a platform.