javascript - 当精灵的运动在 Phaser 3 中停止时如何停止动画
问题描述
发布这个来分享我是如何解决这个问题的。我到处搜索,似乎找不到有效的解决方案。
问题(我的版本) - 我有一个精灵将成为我游戏中的玩家。我有一个向左奔跑和向右奔跑的动画。这些动画被设置为循环播放,因此当他们的速度增加时,玩家会看起来好像他们正在运行。我希望玩家的动画在他们停止移动后停止
解决方案
我的实现
update() {
//If right arrowkey is pressed
if (this.arrowKeys.right._justDown) {
//Move player
this.hero.setVelocityX(5);
//Play your animation animation
this.hero.anims.play('yourAnim', true);
};
//This will run every time the animation "repeats", meaning it has gone through all of its frames. Make sure your animation is set to loop!
this.hero.on("animationupdate", () => {
//If the player has stopped moving
if (this.hero.body.velocity.x < 0.5 && this.hero.body.velocity.x > -0.5) {
//Stop the animation
this.hero.anims.stop();
}
});
}
格式有点差。几乎可以肯定,有一种稍微快一点的方法可以做到这一点。但这是我现在的实现,我只是想分享一下,以防其他人遇到同样的问题!随意评论更好的解决方案,或任何问题
推荐阅读
- c# - Blazor WebAssembly 应用程序 - 来自浏览器的 API 调用 - 抱歉,此地址没有任何内容
- c++ - 按钮和文本重叠
- c# - C# 中的远程桌面客户端访问图像数据
- java - 如何为 Map 类型的 RequestParam 指定默认值
? - c++ - msvc 等效于函数、参数和局部变量的 __attribute__((unused))
- rust - 使用 const 通过强制转换计算其他 const 表达式
- flutter - 使用循环跳过 StreamBuilder 中的列表项
- javascript - 使用 React-NextJS 模拟 Chrome 扩展的慢速网络连接
- csv - 我可以将 csv 从 url 链接到 google 电子表格吗?
- reactjs - 如何在 create-react-app 中的 web worker 中导入已安装的节点模块?