实现 Double-Jump Phaser 3
Implementing Double-Jump Phaser 3
您好,我正在尝试使用 javascript 和 Phaser 3 库实现双跳,但它不会工作,我无法弄清楚 why.I 已在创建中声明 this.canDoubleJump 为真( ), 这是我的更新函数的代码。
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);
}
}
编辑:
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;
}
解决方案:
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)
}
}
您的代码不起作用的原因:
- 用户按下space栏,例如持续 0.3 秒
didPressJump
是真的,this.player.body.onFloor()
是真的。玩家跳跃
- 一帧之后,例如1/30 = 0.0333 秒,
didPressJump
仍然为真,但 this.player.body.onFloor()
为假。玩家在下一帧双跳。
解决方案:
双跳只能在用户释放space条后触发。
还有一个“bug”。当玩家坠落 (!this.player.body.onFloor()
)) 但之前没有跳跃时,二段跳是不可能的。但是如果玩家跳跃,落在地板上然后坠落,则可以进行二段跳。
您好,我正在尝试使用 javascript 和 Phaser 3 库实现双跳,但它不会工作,我无法弄清楚 why.I 已在创建中声明 this.canDoubleJump 为真( ), 这是我的更新函数的代码。
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);
}
}
编辑:
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;
}
解决方案:
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)
}
}
您的代码不起作用的原因:
- 用户按下space栏,例如持续 0.3 秒
didPressJump
是真的,this.player.body.onFloor()
是真的。玩家跳跃- 一帧之后,例如1/30 = 0.0333 秒,
didPressJump
仍然为真,但this.player.body.onFloor()
为假。玩家在下一帧双跳。
解决方案:
双跳只能在用户释放space条后触发。
还有一个“bug”。当玩家坠落 (!this.player.body.onFloor()
)) 但之前没有跳跃时,二段跳是不可能的。但是如果玩家跳跃,落在地板上然后坠落,则可以进行二段跳。