0

Hi I am trying to implement a double jump using javascript and phaser 3 library but it wont work and i cant figure out why.I have declared this.canDoubleJump as true in create(), Here is the code to my update function.


        const didPressJump = this.cursors.space.isDown;

        if (didPressJump) {
            if (this.player.body.onFloor()) {
                this.canDoubleJump = true;
                this.player.play('pablo_jump', true);
                this.player.body.setVelocityY(-200);
            } else if (this.canDoubleJump) {
                this.canDoubleJump = false;
                this.player.body.setVelocityY(-200);
            }
        }

EDIT:

        let justPressedJump = this.cursors.space.isDown;
        
        if (!justPressedJump && this.player.body.onFloor()) {
            this.player.allowedToJump = true;
        }
        
        if (justPressedJump && this.player.body.onFloor() && this.player.allowedToJump) {
           
            this.canDoubleJump = true;
            this.player.body.setVelocityY(-200);
            this.player.allowedToJump = false;
        }
        else if(this.canDoubleJump){
            console.log(this.canDoubleJump)
            this.player.body.setVelocityY(-200);
            this.canDoubleJump = false;
        }

SOLUTION:

        const didPressJump =Phaser.Input.Keyboard.JustDown(this.cursors.space);

        if (didPressJump) {
          if (this.player.body.onFloor()) {
            this.canDoubleJump = true;
            this.player.body.setVelocityY(-200);
            this.player.play('pablo_jump', true)
          } else if (this.canDoubleJump) {
            this.canDoubleJump = false;
            this.player.body.setVelocityY(-200);
            this.player.play('pablo_double_jump', true)
          }
        }

1 Answer 1

2

The reason your code doesn't work:

  1. The user presses space bar, e.g. for 0.3 seconds
  2. didPressJump is true, this.player.body.onFloor() is true. Player jumps
  3. After one frame, e.g. 1/30 = 0.0333 seconds, didPressJump is still true but this.player.body.onFloor() is false. Player double jumps in the very next frame.

Solution:

Double jump can only be triggered after user released space bar.

There is also another "bug". When the player is falling (!this.player.body.onFloor())) but didn't jump before, double jump is not possible. But if player jumps, lands on the floor and then is falling, a double jump is possible.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks I understand why its not working now Im just trying to find a different way to test the input. If i use onDown or onUp he just jumps over and over. Cant find any onrelease or anything in the docs...
@BenMarr If there is no onrelease you can create your own onrelease. Store the previous state of didPressJump and compare it with the current state.
@BenMarr I think you should ask a new question. My answer answers your initial question. Another answer could answer your edit and this becomes confusing.
It answers the question but i still cant get the double jump to work. I will post a solution if i find it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.