Reputation: 125
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)
}
}
Upvotes: 0
Views: 330
Reputation:
The reason your code doesn't work:
didPressJump
is true, this.player.body.onFloor()
is true. Player jumpsdidPressJump
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.
Upvotes: 2