[英]How to stop animations when a sprite's movement stops in Phaser 3
发布这个来分享我是如何解决这个问题的。 我到处搜索,似乎找不到有效的解决方案。
问题(我的版本) - 我有一个精灵将成为我游戏中的玩家。 我有一个左跑和右跑 animation。 这些动画被设置为循环播放,因此当他们的速度增加时,玩家将看起来好像他们正在运行。 我希望玩家的 animation 一旦停止移动就停止
我的实现
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();
}
});
}
格式有点差。 几乎可以肯定,有一种稍微快一点的方法可以做到这一点。 但这是我现在的实现,我只是想分享一下,以防其他人遇到同样的问题,请随时评论更好的解决方案或任何问题
我遇到了这样的问题,您可能不再需要解决方案,但其他人可能需要。 我用 JustDown 启动 animation 和 JustUp 到 go 到我空闲的 state。 更新中的代码:
if(Phaser.Input.Keyboard.JustDown(downKey)){
gamePiece.anims.play("down");
}
if(Phaser.Input.Keyboard.JustUp(downKey)){
gamePiece.anims.play("spin");
}
在您的情况下,它将停止 animation 而不是空闲的 animation。
我认为您正在寻找.pause()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.