Nathan
Nathan

Reputation: 536

How to use currentFrame/totalFrames Properly AS3

Hey everyone been researching this for a bit now and can't seem to figure it out.

Basically what I'm trying to do is when my introScreen is the done with its animation I want to remove the introScreen and add the startScreen because both Movie Clip object contain animations and I dont want one to start while the other is still going on. So I am trying to use the Frame properties to check this condition and execute it.

This is what I have so far but it does nothing but play the introScreen and just stays on that screen.

//Start Game Screen
        introScreen = new mcIntroScreen();
        stage.addChild(introScreen);
        introScreen.x = (stage.stageWidth / 2);
        introScreen.y = (stage.stageHeight / 2);

        if (introScreen.currentFrame == introScreen.totalFrames)
        {
            stage.removeChild(introScreen);

            //Start Game Screen
            startScreen = new mcStartGameScreen();
            stage.addChild(startScreen);
            startScreen.x = (stage.stageWidth / 2);
            startScreen.y = (stage.stageHeight / 2);
            trace(startScreen + "startscreenADDED");
            startScreen.addEventListener("PLAY_AGAIN", playGameAgain, false, 0, true);

            startScreen.addEventListener("TIME_ATTACK", timeAttackMode, false, 0, true);
        }

Maybe someone can help clear this up for me I thought the current code means that if the current frame plays through and reaches the total frames in the animation then the condition would be satisfied I tried tracing it as well but nothing comes up. Any help would be appreciated.

Upvotes: 0

Views: 1984

Answers (1)

Aaron Beall
Aaron Beall

Reputation: 52173

You need to put the frame check within an Event.ENTER_FRAME handler. As it is now, unless you left out some code, it only checks once immediately after it creates the introScreen, which of course means its still on frame 1. Try something like this:

// Start Game Screen
introScreen = new mcIntroScreen();
// etc

addEventListener(Event.ENTER_FRAME, introEnterFrameHandler);

function introEnterFrameHandler(e:Event):void {
    if (introScreen.currentFrame == introScreen.totalFrames)
    {
        removeEventListener(Event.ENTER_FRAME, introEnterFrameHandler);
        stage.removeChild(introScreen);
        // etc
    }
}

Note that this will mean the very last frame won't really be seen. You could use Event.EXIT_FRAME to let the last frame display before handling the ending.

Also, the way I usually do this is a little different. Instead of tracking the playback with Event.ENTER_FRAME I dispatch an event at the end of my timeline. Example:

// on the last frame of the symbol timeline
dispatchEvent(new Event("introComplete", true));

Then I can simply handle it as a custom event:

// Start Game Screen
introScreen = new mcIntroScreen();
// etc
addEventListener("introComplete", introCompleteHandler);
function introCompleteHandler(e:Event):void {
    stage.removeChild(introScreen);
    // etc
}

Upvotes: 2

Related Questions