Callum Singh
Callum Singh

Reputation: 79

AS3 Flash - toggle animation on KeyPress

When my game character is hitting the computers, and spacebar is pressed the animation changes to gotoAndStop(2). This is working so far.

What i want is so that when i press the spacebar again it goes back to gotoAndStop(1)

How would i go about doing this?

Here's my code:

public function turnOn(event:KeyboardEvent)
    {
        if(jon.hitTestObject(computers) && event.keyCode == 32)
        {
            computers.gotoAndStop(2);
        }

    }

Upvotes: 0

Views: 218

Answers (1)

Daniil Subbotin
Daniil Subbotin

Reputation: 6718

Check current frame of computers: computers.currentFrame.

Your code will be looks like:

if(jon.hitTestObject(computers) && event.keyCode == 32)
{
    if (computers.currentFrame == 1)
        computers.gotoAndStop(2);
    else if (computers.currentFrame == 2)
        computers.gotoAndStop(1);
}

Upvotes: 2

Related Questions