0
votes

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.

2
I'd like to help you out but I think I'm going to need some more information. When flash glitches does you game completely stop? Are there any errors in the console? Could you provide some more code? Or even better post the FLA somewhere, I'd be willing to take a look at the problem as a whole and offer any ideas. - Andrew Sellenrick
Thank you for the fast reply! Here's the .swf I'll upload the FLA. one second please. computing.northampton.ac.uk/~13422770/gameGroup/KD.swf - Moynul
P.S A to attack, key arrows to move. - Moynul

2 Answers

1
votes

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.

1
votes

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. i.e. 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.