[英]How to stop animations when a sprite's movement stops in Phaser 3
Posting this to share how I tackled this problem.发布这个来分享我是如何解决这个问题的。 I had searched everywhere and couldn't seem to find a working solution.
我到处搜索,似乎找不到有效的解决方案。
The problem (my version) - I had a sprite that was going to be my player in my game.问题(我的版本) - 我有一个精灵将成为我游戏中的玩家。 I had both a running left and a running right animation.
我有一个左跑和右跑 animation。 These animations are set to loop, so the player will appear as if they are running when their velocity is increased.
这些动画被设置为循环播放,因此当他们的速度增加时,玩家将看起来好像他们正在运行。 I wanted the player's animation to stop once they had stopped moving
我希望玩家的 animation 一旦停止移动就停止
My implementation我的实现
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();
}
});
}
Formatting is a little bad.格式有点差。 And there's almost surely a slightly quicker way to do this.
几乎可以肯定,有一种稍微快一点的方法可以做到这一点。 But this is my implementation for now, and I just wanted to share in case someone else was having the same issue, Feel free to comment with a better solution, or any questions
但这是我现在的实现,我只是想分享一下,以防其他人遇到同样的问题,请随时评论更好的解决方案或任何问题
I had a problem like this, you might not need a solution anymore, but others might.我遇到了这样的问题,您可能不再需要解决方案,但其他人可能需要。 I used JustDown to start the animation and JustUp to go to my idle state.
我用 JustDown 启动 animation 和 JustUp 到 go 到我空闲的 state。 Code in update:
更新中的代码:
if(Phaser.Input.Keyboard.JustDown(downKey)){
gamePiece.anims.play("down");
}
if(Phaser.Input.Keyboard.JustUp(downKey)){
gamePiece.anims.play("spin");
}
In your case, it would stop the animation instead of an idle animation.在您的情况下,它将停止 animation 而不是空闲的 animation。
I think you're looking for the .pause()
method.我认为您正在寻找
.pause()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.