[英]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.