I am working on a simple scrolling game. When the right key is pressed, the background scrolls and the character MC advances to the second frame of the man MC which contains an animation of the character walking. When the key is released, the background stops scrolling and the character returns to frame one of the man MC which he is just standing.
Code:
stop();
function man_walks(e:KeyboardEvent):void {
if (e.keyCode==Keyboard.RIGHT) {
man_mc.gotoAndStop(2);
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, man_walks);
function stop_man(e:KeyboardEvent):void {
man_mc.gotoAndStop(1);
}
stage.addEventListener(KeyboardEvent.KEY_UP, stop_man);
The problem I am having is that the walking animation does not play completely or loop. It plays for a few frames then stops.
I think the problem is that the script (gototAndStop(2); ) repeats as long as the key is pressed, meaning it continues to return and stop on frame 2 causing the walk cycle to pause.
Is this what is causing the walk cycle not to animate? How do I get around this?
I’ve attached a small example showing the problem.
stop();
function man_walks(e:KeyboardEvent):void {
if (e.keyCode==Keyboard.RIGHT) {
if (man_mc.currentFrame != 2) {
man_mc.gotoAndStop(2);
}
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, man_walks);
function stop_man(e:KeyboardEvent):void {
man_mc.gotoAndStop(1);
}
stage.addEventListener(KeyboardEvent.KEY_UP, stop_man);
KeyboardEvent.KEY_DOWN gets called over and over again whenever a key is held down. So you keep sending the movieclip to frame 2 every few hundred milliseconds. So add an if statement to check whether the movieclip is already at frame 2 before sending it to frame 2. If it's already there, don't do anything to it.