You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Phaser中多按键判断的If else语句无法播放动画问题

问题原因

你的代码存在条件判断顺序逻辑错误:当同时按住Shift和右键时,程序会先命中else if (cursors.right.isDown)分支(因为它排在Shift+右键的判断之前),直接执行行走动画的逻辑,导致后面的奔跑判断完全不会被触发。

修复方案

把Shift+右键的判断分支移到单独右键的分支前面,让程序优先检测奔跑组合键的条件;同时记得给奔跑状态设置更快的移动速度,让逻辑更合理:

function handlePlayerControls() {
    
    if (cursors.left.isDown)
    {
        player.setVelocityX(-160);
        player.anims.play('walkLeft', true);
    }
    else if (cursors.shift.isDown && cursors.right.isDown) {
        // 优先判断奔跑组合键
        player.setVelocityX(240); // 可根据需求调整奔跑速度
        player.anims.play('runRight', true)
    }
    else if (cursors.right.isDown)
    {
        player.setVelocityX(160);
        player.anims.play('walkRight', true)
    }
    else
    {
        player.setVelocityX(0);
        player.anims.play('idle', true);
    }
    
    if (cursors.up.isDown && player.body.blocked.down)
    {   
        player.setVelocityY(-330);
    }
    
 }
额外说明
  • 条件判断是从上到下依次匹配的,优先级高的逻辑(比如组合键操作)必须放在前面,否则会被低优先级的条件覆盖。
  • 奔跑状态对应更快的移动速度是常规游戏逻辑,你可以根据自己的需求调整速度数值。

内容的提问来源于stack exchange,提问作者Jordan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 03:45:30