Easy Double Jump

J

Jordan Robinson

Guest
Easy Double Jump
Jordan Robinson

GM Version: 1.4.1757
Target Platform: ALL
Download: N/A
Links: N/A

Summary:
I am creating this tutorial specifically because I have seen many people trying to do this with lots of variables or alarms. In this tutorial, you will learn how to implement the most simple double jump system into your platformer game. Not only that, however, but this system is extremely versatile and will allow you to quickly change the number of jumps allowed from 0 to infinity with a single variable.

Tutorial:
Let's first discuss how the system is going to work. Our player object will have two variables related to the system. These will be jump_number and jump_current.

jump_number is how many times the player can jump. So a value of 0 means no jumping, 2 means double jumping, and so on.

jump_current is how many jumps the player currently has left. When he is standing on the ground this will be equal to jump_number. But once the user starts pressing the jump key, each jump will subtract 1 off jump_current until it reaches 0.

It's a pretty straightforward system, so we can jump into the code now. My examples are going to be based on the platformer movement code by Shaun Spalding. This is partly because it is really clear and easy to understand code, but also because I imagine a lot of people following this tutorial will have similar code for their movement systems which will make implementing this a whole lot easier.

Code:
grav    = 0.4;
ysp     = 0;
Code:
ysp += grav;

if(place_meeting(x, y + ysp, obj_solid))
{
    while(!place_meeting(x, y + sign(ysp), obj_solid))
    {
        y += sign(ysp);
    }
 
    ysp = 0;
}

y += ysp;

So this is the sort of thing you should have set up. If you don't know how this works, check out Shaun Spalding's tutorial.

This is what the step event should look like, with our two multi-jump system variables added:
Code:
grav            = 0.4;

jump_number     = 2; //How many jumps the player can make
jump_current    = 0; //How many jumps the player has remaining

ysp             = 0;
And finally, the step event:
Code:
ysp += grav;

if(keyboard_check_pressed(vk_space) && jump_current > 0)
{
    ysp = -8;
    jump_current--;
}

if(place_meeting(x, y + ysp, obj_solid))
{
    while(!place_meeting(x, y + sign(ysp), obj_solid))
    {
        y += sign(ysp);
    }
 
    if(ysp > 0)
    {
        jump_current = jump_number;
    }
 
    ysp = 0;
}

y += ysp;
So here you can see that when the user presses the jump key, it checks if jump_current is more than 0. If it is, then it allows the player to jump. It also deducts 1 from jump_current.
The next really important part is in the collision code. First, it checks whether the ysp is greater than 0. This prevents the player jumping into the ceiling to reset their jump_current variable. It then resets jump_current to be the same as jump_number so that the system can reset.

Having the variable jump_number is incredibly useful. You can use it make unlockable abilities, by having a default of 1 until the player reaches the next level for example. You can also temporarily stop the player from being able to jump by setting it to 0, or even allowing the player to jump as much as they like!

Hopefully you found this tutorial useful. Cheers.
 
Last edited by a moderator:
E

Ezeeskillz

Guest
Hi Jordan,

Thank you not only for this but also your pixel perfect movement and jumping code. I have been using Shaun's movement code up until now and really like the way this one feels in comparison. CHEERS!

I am currently using this code combined with the pixel perfect movement code. I successfully added wall jumping by just adding:

if(k_jump && place_meeting(x, y + 1, obj_solid))
{
speed_y = -6
}

if(k_jump && place_meeting(x +1, y, obj_solid))
{
speed_y = -6
speed_x = -2
}

if(k_jump && place_meeting(x -1, y, obj_solid))
{
speed_y = -6
speed_x = +2
}

Is there a cleaner way to write this wall jump code?
Also, with this code if I and not pressing A or D then the player will be rebound off the wall for 2 pixels. However if I am still holding down A or D then the player object does not rebound at all. How would I make it so that when I jump off of the wall the player rebounds and then reacts to holding A or D to move back towards the wall or continue further from it?
 
J

Jordan Robinson

Guest
Hi Jordan,

Thank you not only for this but also your pixel perfect movement and jumping code. I have been using Shaun's movement code up until now and really like the way this one feels in comparison. CHEERS!

I am currently using this code combined with the pixel perfect movement code. I successfully added wall jumping by just adding:

if(k_jump && place_meeting(x, y + 1, obj_solid))
{
speed_y = -6
}

if(k_jump && place_meeting(x +1, y, obj_solid))
{
speed_y = -6
speed_x = -2
}

if(k_jump && place_meeting(x -1, y, obj_solid))
{
speed_y = -6
speed_x = +2
}

Is there a cleaner way to write this wall jump code?
Also, with this code if I and not pressing A or D then the player will be rebound off the wall for 2 pixels. However if I am still holding down A or D then the player object does not rebound at all. How would I make it so that when I jump off of the wall the player rebounds and then reacts to holding A or D to move back towards the wall or continue further from it?
There are different ways of doing it, but I wouldn't say that you need to change it. As for having the player rebound off the wall for several steps and then allowing control again - I would suggest to either have a way of disabling the L/R input or look into finite state machines. These are not as complicated as they sound.
 
P

Palocles

Guest
Can you combine the double jump code with a limitation that the first jump can only be made from a solid surface?

And can that then be combined with a jump buffer?

Edit:
Been thinking about it and it’d go something like this, I think:

If(currentJump == 0 && place_meeting(x, y+1, obj_solid) && keyboard_pressed_check(vk_(jump key))
{jump code, increment currnetJump}
Else if(currentJump <maxJump && jumpKeyPressed)
{jump code, increment currentJump}

There is another case to catch I think but hard to do it all in my head.
 
Last edited by a moderator:
Top