Adele
Adele

Reputation: 321

Double Jump Phaser 3?

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.

I'm at this point:

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) {
        salto();
    } 

}

Upvotes: 1

Views: 465

Answers (1)

John B
John B

Reputation: 20400

It might look something like this:

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 (player.body.touching.down) {
        player.jumpCount = 0;
    }

    var canDoubleJump = player.jumpCount < 2;

    if (cursors.up.isDown && (player.body.touching.down || canDoubleJump ) {
        player.jumpCount++;
        salto();
    }
}

Track the current jumpCount which you increment when the player jumps, and then reset it to 0 when they are touching the ground. They can only jump if they're touching the ground OR they haven't jumped twice already.

I assumed that you can add jumpCount to the player object.

Upvotes: 1

Related Questions