How to Double Jump in a Platformer


How to Double Jump in a Platformer

The Windy Woods template is a great starting point for your own platformer.

In this tutorial, we'll show you how to create a double jump mechanic to make platforming more fun.

Create a new project using the Windy Woods template. You can choose either GML Code or GML Visual:

undefined

You can use your existing Windy Woods project if you already have one.

How to Double Jump in GameMaker

We'll create two variables to keep track of the player's jumps: how many times it can jump, and how many types it has jumped.

Open obj_player's Create event (obj_player is found under Objects -> Characters -> Player).

Add the following code to the end of the event.

current_jumps = 0;
max_jumps = 2;

undefined

current_jumps will track how many times the player has jumped, and resets on landing.

max_jumps is how many times the player is allowed to jump.

You can set max_jumps to 2, 3, 4 etc. to change how many times the player can jump in total, before touching the ground again.

See how these variable names are self-describing: current_jumps and max_jumps.

We could have named them cj and mj, but that would confuse everyone reading the code.

Open obj_player's Key Press - Space event. In the template, it has just one condition for the grounded variable:

undefined
The conditional block in GML Code

undefined
The conditional block in GML Visual

This checks if the player is grounded before allowing it to jump. grounded is a variable set in obj_player's Begin Step event, and is true whenever the player is on the ground.

We need to modify this event to use our new variables, current_jumps and max_jumps.

How?

Change the if statement to check if current_jumps is less than max_jumps.

Inside the if block, increase the current_jumps by one.

if (current_jumps < max_jumps)
{
        current_jumps += 1;

        // existing event code
}

undefined

This allows the player to jump if current_jumps is less than max_jumps, and increases the number of current_jumps each time the player jumps to prevent the player from jumping forever.

How to Reset Jumps

We want to reset current_jumps when the player touches the ground, so they can jump the same amount of times again.

Right click on the obj_player's Begin Step event and choose "Inherit Event".

undefined

Add the following code after the event_inherited(); line or Call Parent Event action:

if (grounded) 
{
        current_jumps = 0;
}


undefined

This code resets the number of times the player has jumped to zero once they've landed on the ground, allowing the player to double jump again.

Bug or Feature?

If we test the game right now, everything works fine.

But we do have a small issue. If you fall off of a ledge, you can still jump two more times.

This might be what you want, and if so, you can leave everything the way it is.

However, you may want the player to "use up" a jump when it falls. This means if you allow two maximum jumps, after falling off a ledge, only one jump should remain.

Counting Fall as Jump

Open obj_player's End Step event. This contains all state transitions, for example, switching the player from “walking” to “falling”.

Find the "case" for spr_player_walk, and then the line where it changes the sprite to spr_player_fall.

In the same block, set current_jumps to 1 as highlighted below.

undefined

undefined

This code sets current_jumps to 1 when the player starts to fall, using up one of the player's jumps.

Now, when the player falls off of a ledge, they can only jump one more time.

Increasing Max Jumps

You can increase the number of jumps that a player can make by increasing max_jumps. For example, here, I've set max_jumps to 4.

A challenge for you:

Create a pickup that increases the player's max_jumps when they collect it.

This will be an exciting power-up for the player to find!

Summary

Here are some key takeaways:

  • You can create a variable to store how many times something has happened, like we did with current_jumps.
  • You can create another variable to limit the first variable, like we did with max_jumps.
  • A simple comparison condition helps you create game logic using such variables, e.g. if (current_jumps < max_jumps).
  • Apply this concept to other parts of your game, where you want something to happen, but only a certain amount of times.

Read more GameMaker tutorials to build your game.

Happy GameMaking!