Phaser - 不允许多键事件

Phaser - Can't allow a multiple key event

我正在编写一个简单的平台游戏,我正在编写和设置玩家的移动。这是我的更新函数:

update: function () {

    game.physics.arcade.collide(this.player, this.platform);
    game.camera.follow(this.player);

        if(this.cursor.right.isDown){
            this.player.body.velocity.x= 200;
            this.player.animations.play('correr', 5, true);
            this.player.scale.x=1;
        }

        else if(this.cursor.left.isDown){
            this.player.body.velocity.x= -200;
            this.player.animations.play('correr', 5, true);
            this.player.scale.x=-1;
        }

        else if(this.jump.isDown && this.player.body.wasTouching.down) {
            this.player.body.velocity.y= -400
        }   

        else if((this.cursor.right.isDown || this.cursor.left.isDown) && this.jump.isDown){
            this.player.body.velocity.x= 200;
            this.player.body.velocity.y=-200;
        }

        else{
            this.player.body.velocity.x = 0;
            this.player.animations.stop();
            this.player.frame = 4;
        }
    }

一切正常,但在我最后的 else if 中假设玩家应该跳跃和行走,但它不起作用!我的意图是玩家可以边走边按跳跃键+左右键跳跃,现在我只能先跳再走。

我不知道为什么最后一个 else 没有被执行,因为我试图将它移动到第一个 if 子句中并且它运行得很好,但我无法确定错误。

感谢您的帮助。

问题出在你的第一对 if/else 语句。

这两位将捕获所有左右方向键。

if (this.cursor.right.isDown) {
    // Code is checked first, and will trigger if the right arrow key is down. Nothing else will trigger.
} else if (this.cursor.left.isDown){
    // Code is checked second, and will trigger if the left arrow key is down. Nothing else will trigger.
}

这意味着这永远不会触发。

} else if ((this.cursor.right.isDown || this.cursor.left.isDown) && this.jump.isDown) {
    // Code will never trigger, since right and left are covered by your first two if/else statements.
}

一种选择是执行多个 if/else 语句。

// here, reset the player velocity
this.player.body.velocity.x = 0;

if (this.cursor.right.isDown) {
    // code
} else if (this.cursor.left.isDown) {
    // code
} else {
    this.player.animations.stop();
    this.player.frame = 4;
{

if (this.jump.isDown && this.player.body.touching.down) {
    // code
} else if (...) {
    // code as needed
}

Phaser 官方教程covers this in part 6.

 this.player.body.velocity.x = 0;

应该是播放器的起始状态,也是更新函数的第一行之一,你可以说这是他的 "idle" 状态。

另外我会推荐跳跃计时器

var jumpTimer = 0; 
...................
if(jump && (player.body.onFloor()||
 player.body.touching.down)&& this.time.now > jumpTimer){

        player.body.velocity.y = -400;
        jumpTimer = this.time.now + 750;
        player.animations.play('jump');
    }

更多相位机制示例,可在此处找到https://gamemechanicexplorer.com/#platformer-4