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

Phaser游戏开发:如何让血条动画在指定帧停止?

Phaser血条动画指定帧停止问题解决方案
  • 问题核心:stopOnFrame确实需要帧对象而非帧号,你用this.hpBar.getFrame()是找错了地方——Sprite没有这个方法,帧对象得从对应的动画实例里获取。

  • 正确获取帧对象的步骤:

    1. 先通过动画key拿到动画实例:this.anims.get('damageAnimation')
    2. 从动画实例的frames数组中按索引取帧,或者用动画实例的getFrameByIndex()/getFrameByName()方法(根据你的动画帧定义方式选择)
  • 修正后的代码示例:

// 获取目标动画实例
const damageAnim = this.anims.get('damageAnimation');
// 计算要停止的帧索引
const targetFrameIdx = this.hp - amountOfDamage;
// 拿到对应的帧对象
const targetFrame = damageAnim.frames[targetFrameIdx];
// 播放动画并设置停止帧
this.hpBar.play({
  key: 'damageAnimation',
  startFrame: this.hp
});
this.hpBar.stopOnFrame(targetFrame);
  • 额外提醒:
    • 一定要确保targetFrameIdx在动画帧数组的合法范围内(0 到 总帧数-1),不然会拿到undefined导致stopOnFrame失效
    • 如果你的动画是用帧名称而非索引配置的,把damageAnim.frames[targetFrameIdx]换成damageAnim.getFrameByName('你的帧名')
    • Phaser 3里stopOnFrame必须在play()之后调用,不然不会生效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 02:55:45