我的键盘命令有问题,我试图让我的播放器在他左/右行走时播放动画。 键盘为玩家提供移动和播放动画的命令。 问题是他不停地开始动画,并没有让它完成。
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.
更多推荐
发布评论