如何让我的键盘命令让玩家通过动画播放?(How to make my keyboard command make the player play through the animation?)

编程入门 行业动态 更新时间:2024-10-23 11:18:15
如何让我的键盘命令让玩家通过动画播放?(How to make my keyboard command make the player play through the animation?)

我的键盘命令有问题,我试图让我的播放器在他左/右行走时播放动画。 键盘为玩家提供移动和播放动画的命令。 问题是他不停地开始动画,并没有让它完成。

stage.addEventListener(KeyboardEvent.KEY_DOWN,的keyPressed); stage.addEventListener(KeyboardEvent.KEY_UP,keyRelease);

function keyRelease(k:KeyboardEvent) { movement = 0; gotoAndPlay("standing"); } function keyPressed(k:KeyboardEvent) { if(k.keyCode==Keyboard.D) { movement = 5; gotoAndPlay("walking"); } if(k.keyCode==Keyboard.A) { movement = -5; gotoAndPlay("walking"); } }

我想我可以通过所有的代码,并制作一些长的IF列表......

但我想必须有一个更好的方法

I have a problem with my keyboard commands, Im trying to make my player play an animation when he's walking left/right. the keyboard gives the command for the player to move and play the animation. the problem is that he keeps starting the animation endless times, and doesnt let it finish.

stage.addEventListener(KeyboardEvent.KEY_DOWN,keyPressed); stage.addEventListener(KeyboardEvent.KEY_UP,keyRelease);

function keyRelease(k:KeyboardEvent) { movement = 0; gotoAndPlay("standing"); } function keyPressed(k:KeyboardEvent) { if(k.keyCode==Keyboard.D) { movement = 5; gotoAndPlay("walking"); } if(k.keyCode==Keyboard.A) { movement = -5; gotoAndPlay("walking"); } }

I imagine I can go through all the code and make some kind of long list of IFs...

but i figuerd there must be a better way

最满意答案

向键盘添加事件时,一旦按下某个键,如果未释放KeyboardEvent.KEY_DOWN则会不断调度它。 可能发生的是gotoAndPlay("walking")被不断调用,因此总是重新启动动画。

您需要一种方法来检查动画的状态,并避免重新启动序列(如果已经播放)。 就像是:

function keyRelease(k:KeyboardEvent) { movement = 0; gotoAndPlay("standing"); } function keyPressed(k:KeyboardEvent) { if(k.keyCode==Keyboard.D && movement != 5) { movement = 5; gotoAndPlay("walking"); } if(k.keyCode==Keyboard.A && movement != -5) { movement = -5; gotoAndPlay("walking"); } }

这不完全是我推荐它的方式(从当前的移动速度中找出动画状态),但它是一个简单的解决方案,应该适用于这个问题。

When you add events to the Keyboard, once a key is pressed, KeyboardEvent.KEY_DOWN is dispatched constantly if it's not released. What is likely happening is that gotoAndPlay("walking") is being constantly called, therefore always restarting the animation.

You need a way to check the state of the animation and avoid re-starting the sequence if it's already being played. Something like:

function keyRelease(k:KeyboardEvent) { movement = 0; gotoAndPlay("standing"); } function keyPressed(k:KeyboardEvent) { if(k.keyCode==Keyboard.D && movement != 5) { movement = 5; gotoAndPlay("walking"); } if(k.keyCode==Keyboard.A && movement != -5) { movement = -5; gotoAndPlay("walking"); } }

This is not exactly the way I'd recommend it (figuring out the animation state from the current movement speed), but it is a simple solution and should work for this issue.

更多推荐

本文发布于:2023-07-22 10:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219183.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   键盘   动画   让玩家   keyboard

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!