actionscript 3 - How to make my keyboard command make the player play through the animation? -


i have problem keyboard commands, im trying make player play animation when he's walking left/right. keyboard gives command player move , play animation. problem keeps starting animation endless times, , doesnt let 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 can go through code , make kind of long list of ifs...

but figuerd there must better way

when add events keyboard, once key pressed, keyboardevent.key_down dispatched if it's not released. happening gotoandplay("walking") being called, therefore restarting animation.

you need way check state of animation , avoid re-starting sequence if it's being played. 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 not exactly way i'd recommend (figuring out animation state current movement speed), simple solution , should work issue.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -