Double Jump

Hi, I’m new to developing games with Phaser3. I am going crazy, for days I have been looking for a way to do a ‘double jump’ and I can’t …

I would like the player to double jump if I double-click the ‘up’ cursor.
How can I set it so that if I press once the player jumps a certain height and if I press the second time it jumps another height?

 if (cursors.up.isDown && player.body.touching.down){
    player.setVelocityY('100');
}

Try replacing “&& player.body.touching.down” with “&& Phaser.Input.Keyboard.JustDown(cursors.up)”. For each press of the up cursor, you ought to get an additional jump! I have that set up in one of my projects.
EDIT: to clarify, having the “player.body.touching.down” statement makes it so that the player sprite must be touching the platform in order to jump, which would prevent double-jumping, since double-jump is essentially jumping on the air.

1 Like
 if (cursors.up.isDown && Phaser.Input.Keyboard.JustDown(cursors.up)){
    player.setVelocityY('100');
}

Now when I press the up cursor a second time, the player falls fast on the ground and repeats the jump action again…

Ah, try changing your velocity to -100 instead of 100! My bad, didn’t see that before. I think you can also remove the quotation marks from around ‘100’. The origin point in Phaser is the top left corner of the screen, so it goes like this:
(0,0) ----- +X ----> higher X-axis value
|
+Y
|
v
higher Y-axis value

So, when you want to move the player up, you set your Y velocity to a negative number, because you’re actually moving the player closer to the origin of (0,0), so your Y-axis value actually gets smaller. The negative number is basically subtracting from the Y-value. Here’s how I have double-jump set up in my own project:

if (cursors.up.isDown && Phaser.Input.Keyboard.JustDown(cursors.up)) {
this.setVelocityY(-330);
}

2 Likes

It works!! thank you!!!
And do you think I can also set an animation while jumping?
Like:
if (cursors.up.isDown && Phaser.Input.Keyboard.JustDown(cursors.up)) {
this.setVelocityY(-330);
player.anims.play(‘something’);
}

Yes, you can do that! :slight_smile: I’ve been working on some jump animations for my own project, so if you run into any trouble with that, hopefully I can help, LOL.

1 Like

thank you!