Skip to main content
← Make a Platformer

Lesson 2 of 4

Gravity and Jumping

Add one number for up and down speed. The rectangle now has weight. It jumps in a curve and lands.

What you will learn

  • Velocity
  • Gravity
  • Ground collision
  • Stopping the double jump

Before you start

The Game Loop. You use the same two files. update() now does more work.

What this stage does

Click the game and press the up arrow or W to jump. The player rises, slows down, falls, and lands on the ground. You cannot jump again until you land.

How gravity works

Jumping is not "move the player up". It is one number that changes on every frame. velocityY is how fast the player is moving up or down right now.

player.velocityY -= gravity;   // gravity pulls down, so it takes speed away
player.y += player.velocityY;  // move by the current speed

A jump sets velocityY to a large positive number one time. Gravity does the rest of the work. The player rises, slows down, stops, and falls. You never have to write the curve yourself.

Landing, and not jumping twice

isJumping is a true-or-false value. It stops you from jumping again while you are in the air. The code sets it to true when you leave the ground. The code sets it back to false when you land.

mario.js
// Jump only when we are on the ground
if ((keys["ArrowUp"] || keys["KeyW"]) && !player.isJumping) {
    player.velocityY = player.jumpPower;
    player.isJumping = true;
}

// ...after gravity has moved the player:
if (player.y <= groundTop) {
    player.y = groundTop;        // put the feet exactly on the ground
    player.velocityY = 0;        // stop falling
    player.isJumping = false;    // you may jump again
}

Build it step by step

  1. Add the new numbers

    Add jumpPower, velocityY, and isJumping to the player object. Add gravity as a constant near the top of the file. groundTop is already there from the last stage.

  2. Add gravity only

    Add the two gravity lines to update(). Nothing else. Reload. The player falls straight through the floor and disappears. That is correct for now.

  3. Add the floor

    Add the ground check. The player now falls and lands. There is still no jump.

  4. Add the jump

    Add the jump key check. Leave out the !player.isJumping part at first. Hold the up arrow and the player flies. Now add that part and try again. You get one jump. You must land before you can jump again.

  5. Tune the feel

    Change gravity to 0.2, then to 1.5. Change jumpPower to 8, then to 25. Two numbers control how the whole game feels. Spend real time here.

Tune the feel

Two numbers decide how the whole game feels. Try low gravity with a small jump. Then try high gravity with a big jump.

0.6

How much speed gravity adds each frame. A low number makes the player float down slowly.

15

This is the speed the player leaves the ground at.

5

Try these changes

Make these changes in the files beside this guide. Each one only uses what this lesson has already covered. Tick each one when it works.

Check yourself

Three quick questions on this lesson. Get them all right to finish the lesson and earn a pack.

Loading your answers

Teacher's versionHow to run this lesson, what to check, and any answers. Students do not need this.

How to teach it

  • Run step 2 on its own. Let the player fall out of the world. The missing floor is easy to see. The student will name the problem without your help.
  • Let the student fly in step 4 before they add isJumping. Students enjoy the flying player. It also shows them that isJumping is needed, not a random rule.
  • Draw the arc on the board as a list of velocityY values. Write 15, 14.4, 13.8, and keep going down through 0 into negative numbers. Gravity is one subtraction, repeated every frame.
  • Step 5 is not filler. Give it ten minutes. Tuning two numbers is the first time most students feel like designers instead of typists.