Jump to content

Double Jump Phaser 3


Adele
 Share

Recommended Posts

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 found many questions similar to this one here, but none gave me a concrete solution ...

I would like the player to double jump if I double-click the 'up' cursor.

Please help me.

function update() {
 
 
if (cursors.left.isDown) {
player.setVelocityX(-130);
player.anims.play('left', true);
 
} else if (cursors.right.isDown) {
player.setVelocityX(130);
player.anims.play('right', true);
 
} else {
player.setVelocityX(0);
player.anims.play('idle', true);
}
 
if (cursors.up.isDown && player.body.touching.down) {
layer.setVelocityY('100');
}
 
}
Link to comment
Share on other sites

Hi @Adele welcome to html5gamedevs ?. Without seeing the context of your code I cannot help that much. But, I will give it a try. This is how I would implement a double jump on my game.

// create a variable canDoubleJump

function update() {
    
    if (this.keys.left.isDown) {
      this.body.setAccelerationX(-1000);
      player.anims.play('left',true);
    } else if (this.keys.right.isDown) {
      player.body.setAccelerationX(1000);
      player.anims.play('right',true);
    } else {
      player.anims.play('idle',true);
      player.body.setAccelerationX(0);
    }

    const didPressJump = Phaser.Input.Keyboard.JustDown(this.keys.up);

    // player can only double jump if the player just jumped
    if (didPressJump) {
      if (player.body.onFloor()) {
        // player can only double jump if it is on the floor
        this.canDoubleJump = true;
        player.body.setVelocityY(-100);
      } else if (this.canDoubleJump) {
        // player can only jump 2x (double jump)
        this.canDoubleJump = false;
        player.body.setVelocityY(-100);
      }
    }

}

 

Edited by geocine
Link to comment
Share on other sites

  • 2 weeks later...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...