简体   繁体   中英

AS3 key presses cause animation error

I'm making a game and basically I'm having some errors with the animation state.

    public function movementChar()
    {
        if (touchingGround)
        {
            if (rightKey)
            {
                gotoAndStop("run");
                scaleX = 1;

            }
            if (leftKey)
            {
                gotoAndStop("run");
                scaleX = -1;
            }
            if (upKey)
            {
                gotoAndStop("jump");
                this.y -= 15;
                //touchingGround = false;
            }
            if (attackKey)
            {
                gotoAndStop("attack");
            }
            if (!rightKey && !leftKey && !upKey && !attackKey)
            {
                gotoAndStop("stop");
            }
        }

    }       

I have some other coding which says if the player is touching the ground then touchingGround = true;

and if it is true then the player can move right, left, jump and attack.

The problem is that when I press the attack key, it keeps on looping the animation and attacking.

I want the attack key to play the animation once and make the boolean hasAttacked = true; once.

Another problem is that when the player is moving and the attack key has been pressed/ hold down the animation freezes. Flash gets confused on which animation to play so it stops at frame 1 and glitches.

I would appreciate it if someone can give me an idea on how to fix this.

Thank you.

I don't have my flash dev stuffs on my current machine so I can't open the fla but:
I suspect that this is caused by not 'consuming' the key event. ie presumably you are setting rightKey, leftKey, attackKey etc on key up and key down events and checking the state during the update function?
The problem with this is that each update it will act as if pressing the key the first time, every time, you need some method of knowing that the key press has been handled.
As I say I can't check your fla so I can't comment on the best method but I suspect you may have to substantially change your input handling.

Ok I have a few recommendations. First let's address the fact that your attack animation is looping. You will need to go into the SirTimmyAttack movieclip timeline and add

stop();

In to frame 4 to keep the animation from looping forever. After you do that the animation will play once. You have a timer that you are starting when you tell him to attack, after the timer execute he will attack again. So if you hold the "A" key he will ATTACK...Wait for timer...ATTACK...Wait for timer....ATT ect. if you want him only to attack once per press of the "A" key remove that timer and never start it.

On to address your second concern about the player being unable to attack and walk at the same time. That is a technical limitation of the way you are currently animating. Since the players entire animation is contained inside a set of frames you cant mix and match to combine them. I would suggest breaking your player graphics/animations into to halves. An Upper body, and a lower body. That way you can trigger walking/running/standing animations independently from the upperbody/attack animations.

Hope this helps, let me know if you have any other questions.

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