Copy link to clipboard
Copied
I created a movieclip named stickman. In that, I created an animation by drawing a sequence of move in everyframe, so that stickman can run. Now what I want is that when I press a key, the stick man will run from left to right and when I release the key, it will stop. This is my code:
RunningMan.stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN,keypresseddown);
function keypresseddown(event:KeyboardEvent😞void
{
var key:uint = event.keyCode;
switch (key) {
case Keyboard.LEFT :
{
RunningMan.play();
RunningMan.x-=10;
RunningMan.scaleX=-1;
};
case Keyboard.RIGHT :
{
RunningMan.play(); //play animated run
RunningMan.x+=10;
RunningMan.scaleX=1;
};
default: RunningMan.stop();
}
}
However, when I pressed and held a key, it moved from left to right without animated run.
How can I fix it? Thanks in advance.
Copy link to clipboard
Copied
case conditions need a break() statement at the end:
switch (key) {
case Keyboard.LEFT :
RunningMan.play();
RunningMan.x-=10;
RunningMan.scaleX=-1;
break();
case Keyboard.RIGHT :
RunningMan.play(); //play animated run
RunningMan.x+=10;
RunningMan.scaleX=1;
break();
default: RunningMan.stop();break();
}
Copy link to clipboard
Copied
Thanks, but when I pressed a key, the man moved without animation.
Copy link to clipboard
Copied
Make sure that the animatuion (the running part) is triggered with Runningman.play().
Find more inspiration, events, and resources on the new Adobe Community
Explore Now