Double Jump mechanic not working

Hello. I’m having some issues with my double jump power-up. Currently I have an overlap method set up to check for overlap between my player and my double-jump object. It make sense to me that if I use the djPowerUp() function I’ve created to modify the value for doubleJump that when I jump the first time, and my player is touching down, and my jump count is 0, then I’ll jump once (which I do, this works). But then, if I have this power-up and doubleJump is set to true, when it sees that my cursor is down, that I’m not touching down, !this.ball.body.touching.down and my jumpCount is 1, that it will then allow me to jump again. But it isn’t. Is there a better way to do this? Or what could I possibly be doing wrong?

if (this.cursors.jump.isDown && this.ball.body.touching.down && this.jumpCount === 0) {
        this.ball.setVelocityY(-625);
        this.jumpCount++;
    } else if (this.cursors.jump.isDown && !this.ball.body.touching.down && this.doubleJump === true && this.jumpCount === 1) {
        this.ball.setVelocityY(-625);
        this.jumpCount++;
    } else {
        this.jumpCount = 0;
    }
}

djPowerUp(ball, life) {
    this.doubleJump = true;
    this.physics.add.collider(this.life, this.ball);
    life.disableBody(true, true);
    console.log("Double Jump!");
}

One of the potential problems here is that isDown is true on each frame that the button is pressed. You almost certainly aren’t pressing the jump button for only one frame, so you’ll jump off the ground on one frame, then use up your double jump on the very next frame, essentially wasting it. You could try using Phaser.Input.Keyboard.JustDown to detect only the first frame of input.

On another note, why are you re-adding the Collider in djPowerUp? I don’t see it’s necessary, especially if this.life is the life object whose Body you’re disabling.

1 Like

@Telinc1 thanks! I’ll give this a go later today. I understand what you’re asking and it makes sense. When I did a console.log(this.jumpCount) I could see that just a single jump was continuously refreshing the value of jumpCount.

Regarding the Collider, I can definitely remove that. I’m new to Phaser and have only worked in it for about 4 days, so there are still lots of basic quirks I’m having to figure out and fix with my code.

I’ll let you know what the result is when I get a chance to implement the Phaser.Input.Keyboard.JustDown

@Telinc1

I Implemented JustDown but I’m still having the same issue. Even though all of my conditions are being met once I obtain my power up, it will not allow my player to jump a second time once we are in the air. Here is some console output I put together when I jump without the powerup and then the values after I obtain the powerup.

Before:
image

After:
image

Here is my updated code as well with my current setup:

if (Phaser.Input.Keyboard.JustDown(this.cursors.jump) && this.ball.body.touching.down && this.jumpCount === 0) {
        this.ball.setVelocityY(-625);
        this.jumpCount++;
        console.log("JumpCount: " + this.jumpCount);
        console.log("doubleJumpActive: " + this.doubleJumpActive);
    } else if (!this.ball.body.touching.down && Phaser.Input.Keyboard.JustDown(this.cursors.jump) && this.doubleJumpActive === true && this.jumpCount === 1) {
        this.ball.setVelocityY(-625);
        this.jumpCount++;

        console.log("Success");
    } else {
        this.jumpCount = 0;
    }
}

doubleJump(ball, life) {
    this.doubleJumpActive = true;
    life.disableBody(true, true);
    console.log("Double Jump!");
}

Hey Benstrocity,
You’re most likely encountering this issue because you are setting this.jumpcount = 0 in your update function if this.cursors.jump is not JustDown. This will make it so your condition of, this.jumpCount === 1 is never met.

Try this instead:

if (Phaser.Input.Keyboard.JustDown(this.cursors.jump) && this.ball.body.touching.down && this.jumpCount === 0) {
        this.ball.setVelocityY(-625);
        this.jumpCount++;
        console.log("JumpCount: " + this.jumpCount);
        console.log("doubleJumpActive: " + this.doubleJumpActive);
    } else if (!this.ball.body.touching.down && Phaser.Input.Keyboard.JustDown(this.cursors.jump) && this.doubleJumpActive === true && this.jumpCount === 1) {
        this.ball.setVelocityY(-625);
        this.jumpCount++;

        console.log("Success");
    } else if(this.ball.body.touching.down) {
        this.jumpCount = 0;
    }
}

doubleJump(ball, life) {
    this.doubleJumpActive = true;
    life.disableBody(true, true);
    console.log("Double Jump!");
}
1 Like

Still no luck with this. This one has me stumped real good. I hate that I’m not getting any errors or anything, it just isn’t wanting to work. When I’m on the ground my jumpCount is set to 0. After the first jump I am set to 1, I have my doubleJumpActive set to true, so I should be firing on the proper else if statement.

Jake.Caron’s post is absolutely valid, but there’s something else you haven’t considered. Looking at the documentation for JustDown (linked in my previous post):

You can only call justDown once per key press. It will only return true once, until the Key is released and pressed down again.

When your first if statement is evaluated, you call JustDown and you lose the Key’s state, so your second statement ends up always evaluating to false. You should first store JustDown's return value in a local variable in order to check it more than once (I’d have recommended that even if the restriction it works around didn’t exist because you wouldn’t be calling the function twice). Alternatively, you could re-arrange the if statements so that you only call JustDown once (the first and the third conditional can be nested into one big if statement which checks touching.down, while the second could be placed into the big statement’s else clause) - this will also avoid checking touching.down so many times, but it’s more complicated than just using a variable.

Actually, you should also be able to re-arrange the logical operators (&&) in your current code so that JustDown is called last. The other conditions for the first two if statements cannot be simultaneously true, so you’ll avoid the JustDown call which messes up the double jump.

For future mistakes in logic like this one, you should look into the way the browser’s debugger works. It’ll allow you to look at your code expression by expression while it’s running and catch these problems.

2 Likes

Ah! That makes sense. I read the documentation on JustDown but that lightbulb didn’t click right away. I really appreciate all of your help. I’ll give these suggestions a try and follow up with the results. Changing the logical operators order sounds like it should work.

Brilliant! It works perfectly. Thanks again for sticking with me.