Problems with animating character movement
Hi!
I am pretty new to as3, and so I have encountered a problem that I cannot solve by my self.
I have searched other forums and this to see if I can find a solution, but have not found one yet.
I'm making a game where this character is supposed to move left, right,up and down. And so I've animated her walking in all different directions,
and also made the corresponding animations play whenever the right buttons are pushed.
Before I go any further, here's the code so far:
stop();
character.stop();
var vx=0;
var vy=0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
stage.addEventListener(Event.ENTER_FRAME, onEnterThisFrame);
function onKeyPress(e:KeyboardEvent):void{
if(e.keyCode==Keyboard.DOWN){
vy=3;
character.gotoAndPlay("frontWalk");///character is a movieclip with different animated movieclips inside it.
}
else if(e.keyCode==Keyboard.UP){
character.gotoAndPlay("backWalk");
vy=-3;
}
else if(e.keyCode==Keyboard.RIGHT){
character.gotoAndPlay("leftWalk");
vx=3;
}
else if(e.keyCode==Keyboard.LEFT){
character.gotoAndPlay("rightWalk");
vx=-3;
}
}
function onKeyRelease(e:KeyboardEvent):void{
if(e.keyCode==Keyboard.DOWN){
character.gotoAndStop("frontStand");
vy=0;
}
else if(e.keyCode==Keyboard.UP){
character.gotoAndStop("backStand");
vy=0;
}
else if(e.keyCode==Keyboard.RIGHT){
character.gotoAndStop("leftStand");
vx=0;
}
else if(e.keyCode==Keyboard.LEFT){
character.gotoAndStop("rightStand");
vx=0;
}
}
function onEnterThisFrame(e:Event):void{
character.x+=vx;
character.y+=vy;
}
The code works fine if I hold a arrow key down, release it and wait for a second before i push the arrow next key. But if I don't wait a while before I push the next key, it will move the character with the standing still frame for a second and then animate the right walking animation.
Its like, the code can't catch up or something.
I hope I am making sense.
I would highly appreciate any help on this matter.
