# Double Jump mechanic not working

**URL:** <https://phaser.discourse.group/t/double-jump-mechanic-not-working/2659>\
**Category:** Phaser 3\
**Created:** [June 4, 2019, 11:29am UTC](https://phaser.discourse.group/t/double-jump-mechanic-not-working/2659 "2019-06-04T11:29:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benstrocity](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@Benstrocity](https://phaser.discourse.group/u/Benstrocity)\
**Post date:** [June 4, 2019, 11:29am UTC](https://phaser.discourse.group/t/double-jump-mechanic-not-working/2659/1 "2019-06-04T11:29:23Z")

</div>

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!");
}
```

---

<div class="post-metadata">

**Author:** ![Telinc1](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/telinc1/32/60_2.png) [@Telinc1](https://phaser.discourse.group/u/Telinc1)\
**Post date:** [June 4, 2019, 12:01pm UTC](https://phaser.discourse.group/t/double-jump-mechanic-not-working/2659/2 "2019-06-04T12:01:18Z")

</div>

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`](https://photonstorm.github.io/phaser3-docs/Phaser.Input.Keyboard.html) 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.

---

<div class="post-metadata">

**Author:** ![Benstrocity](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@Benstrocity](https://phaser.discourse.group/u/Benstrocity)\
**Post date:** [June 4, 2019, 12:34pm UTC](https://phaser.discourse.group/t/double-jump-mechanic-not-working/2659/3 "2019-06-04T12:34:06Z")

</div>

@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`

---

<div class="post-metadata">

**Author:** ![Benstrocity](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@Benstrocity](https://phaser.discourse.group/u/Benstrocity)\
**Post date:** [June 5, 2019, 9:52am UTC](https://phaser.discourse.group/t/double-jump-mechanic-not-working/2659/4 "2019-06-05T09:52:36Z")

</div>

@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](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/2/27b7acbc22a47f223c190de5308070b52ff329b3.png)

After:  
 ![image](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/7/7a50713b33690a60be4299e854cff75f3221242a.png)

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!");
}
```

---

<div class="post-metadata">

**Author:** ![Jake.Caron](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jake.caron/32/1841_2.png) [@Jake.Caron](https://phaser.discourse.group/u/Jake.Caron)\
**Post date:** [June 5, 2019, 1:28pm UTC](https://phaser.discourse.group/t/double-jump-mechanic-not-working/2659/5 "2019-06-05T13:28:31Z")

</div>

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:

```javascript
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!");
}

```

---

<div class="post-metadata">

**Author:** ![Benstrocity](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@Benstrocity](https://phaser.discourse.group/u/Benstrocity)\
**Post date:** [June 5, 2019, 5:54pm UTC](https://phaser.discourse.group/t/double-jump-mechanic-not-working/2659/6 "2019-06-05T17:54:59Z")

</div>

> [@Jake.Caron](#):
>
> } else if (!this.ball.body.touching.down && Phaser.Input.Keyboard.JustDown(this.cursors.jump) && this.doubleJum

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.

---

<div class="post-metadata">

**Author:** ![Telinc1](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/telinc1/32/60_2.png) [@Telinc1](https://phaser.discourse.group/u/Telinc1)\
**Post date:** [June 5, 2019, 7:58pm UTC](https://phaser.discourse.group/t/double-jump-mechanic-not-working/2659/7 "2019-06-05T19:58:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Benstrocity](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@Benstrocity](https://phaser.discourse.group/u/Benstrocity)\
**Post date:** [June 5, 2019, 8:23pm UTC](https://phaser.discourse.group/t/double-jump-mechanic-not-working/2659/8 "2019-06-05T20:23:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Benstrocity](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@Benstrocity](https://phaser.discourse.group/u/Benstrocity)\
**Post date:** [June 5, 2019, 10:06pm UTC](https://phaser.discourse.group/t/double-jump-mechanic-not-working/2659/9 "2019-06-05T22:06:25Z")

</div>

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