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)
}
}