thor625
thor625

Reputation: 87

Walking animation while moving in as3

Ok so i have an movie clip instanced as "man" I have made him so that he has a Idle and Walk frame like this. He can also move left/right. (I also made him attack but that is irrelevant);

[Idle][walk [] The last frame has

gotoAndPlay("walk");

I need it to loop through the "walk" frames until i lift up the left/right key then go back to "idle" position. What happens is that it will go through the loop once and than go to idle even if I haven't lifted the key. This is my code

      var leftKeyDown:Boolean = false;
        var upKeyDown:Boolean = false;
        var rightKeyDown:Boolean = false;
        var downKeyDown:Boolean = false;
        var charSpeed:Number = 7;
        man.addEventListener(Event.ENTER_FRAME, moveChar);
    function moveChar(event:Event):void{
    if(leftKeyDown || rightKeyDown)
{
    man.gotoAndPlay("walk");
}


if(rightKeyDown)
{
    man.x += charSpeed;
    man.scaleX=1;
}
if(leftKeyDown)
{
    man.x -= charSpeed;
    man.scaleX=-1;
}

            }


        stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
        function checkKeysDown(event:KeyboardEvent):void{

            if(event.keyCode == 37 || event.keyCode == 65){
                leftKeyDown = true;


            }

            if(event.keyCode == 38 || event.keyCode == 87){
                upKeyDown = true;
            }
            if(event.keyCode == 39 || event.keyCode == 68){
                rightKeyDown = true;
                    }
            if(event.keyCode == 40 || event.keyCode == 83){
                downKeyDown = true;
            }

        }



        // checking for upkeys
     stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
function checkKeysUp(event:KeyboardEvent):void{
    if(event.keyCode == 37 || event.keyCode == 65){
        leftKeyDown = false;
        man.gotoAndStop("idle");
    }
    if(event.keyCode == 38 || event.keyCode == 87){
        upKeyDown = false;

    }
    if(event.keyCode == 39 || event.keyCode == 68){
        rightKeyDown = false;
        man.gotoAndStop("idle");
    }
    if(event.keyCode == 40 || event.keyCode == 83){
        downKeyDown = false;

    }
}

thanks

Upvotes: 0

Views: 3339

Answers (3)

The_asMan
The_asMan

Reputation: 6403

Just exit out of the function after an if statement passes and if all else fails it is idle.

if(leftKeyDown){
    man.x -= charSpeed;
    man.scaleX=-1;
    man.gotoAndPlay("walk");
    return;
}
if(rightKeyDown){
    man.x += charSpeed;
    man.scaleX=1;
    man.gotoAndPlay("walk");
    return;
}

 man.gotoAndStop("idle");

or if you want another aspect

switch(true){
  case leftKeyDow:
        man.x -= charSpeed;
        man.scaleX=-1;
        man.gotoAndPlay("walk");
        break;
  case rightKeyDown:
        man.x += charSpeed;
        man.scaleX=1;
        man.gotoAndPlay("walk");
        break;
  default:
        man.gotoAndStop("idle");
}

Upvotes: 2

Allan
Allan

Reputation: 3314

One problem I see is your if/else statements.

Currently, if your right key is up, regardless of if your left key is down, the idle animation will be called. It needs to be changed to something like:

if(leftKeyDown || rightKeyDown)
{
    man.gotoAndPlay("walk");
}
else
{
    man.gotoAndStop("idle");
}

if(rightKeyDown)
{
    man.x += charSpeed;
    man.scaleX=1;
}
if(leftKeyDown)
{
    man.x -= charSpeed;
    man.scaleX=-1;
}

Also make sure you have a stop(); call in your animation loop at the end of the walk and idle animations to prevent it playing all the way through.

Upvotes: 0

brenjt
brenjt

Reputation: 16297

Try using bliting. Here are some sites to learn how to do it.

http://www.gotoandlearn.com/play.php?id=140 <- this is a good one!

http://www.gotoandlearn.com/play.php?id=147

http://www.adobe.com/devnet/flash/articles/blitting_mc.html

http://blog.flexwiz.net/spritesheet-animation-in-as3/

You could also use TexturePacker to create your blit/sprite sheets.

http://www.texturepacker.com/

I have used it on many projects and it saves tons of time

Upvotes: 0

Related Questions