Basics: Scrolling Platforms, 2

 Gravity
Stopping at the Platform
Jumping

Before you try the steps below, make sure you have completed the previous tutorial called Basics: Scrolling Platforms – Part 1.

Watch the movie

In this tutorial, you’ll learn how to add jumping to the scrolling platforms you created in the previous tutorial. Watch the movie to see where you’re headed.

Gravity

To create the effect of gravity, you must first create a new variable for all sprites. Let’s name the variable jump speed. (If you don’t know how to create a variable, see the previous tutorial, Basics: Scrolling Platforms – Part 1.)

Then we change the number inside the variable jump speed so that moves continuously downwards. For example, the number inside jump speed might be -1 … -2 … -3 … and so on, getting smaller and smaller over time. Can you figure out how to assemble the commands below so that they have this effect?

basicsScrolling2Gravity

If you’ve gotten the number inside jump speed to become smaller and smaller, you’ve completed the first step towards making gravity. The second step involves making the cat move downwards in coordination with jump speed. To do that, you should add the command that’s circled in red below to your script. Place this command just above the arrow-key commands you added in the previous lesson.

This new command changes the cat’s y-position (its vertical position)  by whatever number is in jump speed. In other words, this command causes the cat to fall. Can you see why?

basicsScrolling2Fall

Click the green flag to try your script! You’ll see that the cat falls off the bottom of the screen.

Stopping at the Platform

For the next step, we’ll make the cat stop falling when it reaches the platform. We tell the cat that when it’s touching color purple, jump speed should change to 1. So the purple platform pushes the number inside jump speed, and also the cat’s y-position, “upwards” momentarily. This “upwards” push exactly balances the gravity that’s pulling the cat “downwards” and therefore makes the cat stop moving.

Can you assemble the commands below and put them into your script so that the cat stops falling at the platform?

basicsScrolling2Fall2

Jumping

There’s just one more thing to do! When you press the up-arrow, the cat should jump. Can you assemble the commands below …

basicsScrolling2JumpSpeed

… and then add them where the red arrow indicates to make the cat jump?

basicsScrolling2Jump

Try your project! If you’ve done everything correctly, the cat should jump when you press the up-arrow!

Extra Credit!

Can you add obstacles that the cat can jump onto but cannot pass through?

Can you add enemies — especially on platforms 2 or 3? The enemies should move with the platforms. When you touch the enemies, the game ends and you lose.

Can you change the game by adding health points so that you must touch a total of 3 enemies before you lose?

Can you add a goal post at the end of the game so that you win if you reach the goal post?

Have fun!