简体   繁体   中英

AS3 Animation stop at frame 1

I'm really new at AS3, I used to be coding in AS2, but for more than a year I don't use Flash or ActionScript. My problem is when I press left or right arrow which is defenied to move the character to right and left the animation just stop at the first frame. The idle animation works fine, but the walk animation starts and stop in frame 1 everytime I press the buttons.

vector.gotoAndPlay("parado");

var leftKeyDown:Boolean = false;
var rightKeyDown:Boolean = false;
var mainSpeed:Number = 7;

vector.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event:Event):void{

    if(leftKeyDown){
        if(vector.currentLabel!="andando"){
            vector.x -= mainSpeed;
            vector.scaleX=-1;
            vector.gotoAndPlay("andando");
        }
    } else {
        if(rightKeyDown){
            if(vector.currentLabel!="andando") {
                vector.x += mainSpeed;
                vector.scaleX=1;
                vector.gotoAndPlay("andando");
            }
        }
    }
}

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

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

    if(event.keyCode == 39){
        rightKeyDown = true;
    }
    }
    stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
    function checkKeysUp(event:KeyboardEvent):void{

    if(event.keyCode == 37){
        leftKeyDown = false;
    }
    if(event.keyCode == 39){
        rightKeyDown = false;
    }
}

FYI: "parado" is my idle animation and "andando" is my walk animation.

It's not stopping at frame 1, it's just being sent back to frame 1 all the time. Consider what happens when you hold down the button for a few seconds:

  • rightKeyDown starts as false. No code in that branch is executed.

  • User holds the right arrow, rightKeyDown becomes true

  • moverChar checks rightKeyDown , sees it's true and sends the character to "andando".

  • moveChar runs again, sees rightKeyDown is true but the character is still at the "andando" frame, so it does nothing.

  • Character goes to frame after "andando".

  • moverChar runs, rightKeyDown is still true, but the frame is not at "andando" anymore, so it resets back to it.

And that repeats during all the time the user is holding down the key, so it appears to be stuck in frames 1 and 2

A few alternatives to fix this problem:


Change the key frame only when the user presses or releases the button, not every frame in between.

function moveChar(event:Event):void{

    if(leftKeyDown){
        vector.x -= mainSpeed;
        // No frame checks or frame changes here.
    }
    [...]

function checkKeysDown(event:KeyboardEvent):void{
    if(event.keyCode == 37){
        leftKeyDown = true;
        vector.scaleX=-1;
        vector.gotoAndPlay("andando");
        // Send the character to the correct frame when the user presses the key.
    }
    [...]

function checkKeysUp(event:KeyboardEvent):void{
    if(event.keyCode == 37){
        leftKeyDown = false;
        vector.gotoAndPlay("parado");
        // Send it back to idle when the user releases the key.
    }
    [...]

Another option is to store each animation in a movieclip by itself and put them in a container movieclip. So there will be only two frames in the character's symbol, one for the idle animation and the other for the walking animation. In your code you use gotoAndStop instead of gotoAndPlay , so it doesn't matter if it's called every frame.


Edit: Also try to group your conditionals.

} else {
    if(rightKeyDown){
        if(vector.currentLabel!="andando") {
            vector.x += mainSpeed;
            vector.scaleX=1;
            vector.gotoAndPlay("andando");
        }
    }
}

can be rewritten as

} else if (rightKeyDown && vector.currentLabel != "andando"){
    vector.x += mainSpeed;
    vector.scaleX=1;
    vector.gotoAndPlay("andando");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM