Respond only to the last pressed direction key. So in checkKeysDown, set all your flags to false before your conditionals. By the way, you should be able to use a switch statement to clear up some of those ugly if statements.
function checkKeysDown(event:KeyboardEvent):void
{
// I added the following lines
leftKeyDown = false;
upKeyDown = false;
rightKeyDown = false;
downKeyDown = false;
if(event.keyCode == 37)
{
leftKeyDown = true;
}
if(event.keyCode == 38)
{
upKeyDown = true;
}
if(event.keyCode == 39)
{
rightKeyDown = true;
}
if(event.keyCode == 40)
{
downKeyDown = true;
}
}
Everything seems working fine. But thanks anyway. I'll learn more about switches later.
Also, how can I animate the sprite? I already have the Link animated sprites.
I created one movieclip, and inside of said movieclip, I created four frames. Each one has a movement animation (up,left,down and right). The frames are properly labeled.
However, when I add "Link.gotoAndPlay("left");" to the code and publish the file, it just shows the first frame of the animation when I press the left key.
Do I need to place all the individual non-animated sprites in the movieclip timeline?
I think your problem is that you are checking your movement logic every frame. So EVERY FRAME you are telling link to go to the first frame of his movement animation.
You need to add 1 boolean variable for each direction link can animate in. Then you do something like the following when your moveright button is pressed:
if (link.movingRight == false) {
link.gotoAndStop("moveRight");
link.movingRight = true;
}
then you need also set that variable to false when you release the button (as well as going to link's stationary frame/animation.
:edit:
actually, you can probably get by with just one variable link.moving depending on how you want to handle concurrent button presses. Pressing right, holding it down and pressing left.
Hmm...but still didn't solve the animation thingie.
How do you animate a sprite, that has 2 frames per animation? I created 4 movieclips, each doing one of Link's running movement (up, down, left right).
Then I created an empty movieclip with 4 frames. Each frame has one movieclip with one movement direction. They are all labeled.
I may be misunderstanding your structure, but I think you have two levels of movieclips. The first goes to a frame which corresponds to a direction. The second is a clip on each directional frame which has an animation. You need to use gotoAndStop on the first level and play on the second level. You'll also want to loop the second level clips.