user2817200
user2817200

Reputation: 1117

Actionscript 3 - Wait till MovieClip animation has finished before executing next line

Say I have this AS3 code:

playButtonContainer.addEventListener(MouseEvent.CLICK, playButtonClicked);

function playButtonClicked(evt:MouseEvent) {
    instructionsContainer.gotoAndPlay(2);
    analysisScreenContainer.gotoAndPlay(2);
    playButtonContainer.gotoAndPlay(2); 
    statusDescriptionContainer.gotoAndPlay(2);
}

Inside instructionsContainer, analysisScreenCnotainer, playButtonContainer and statusDescriptionContainer is a MovieClip which has its own timeline and animation.

As you can see, I have 4 different MovieClips playing one after another. How do I make it so that

statusDescriptionContainer.gotoAndPlay(2);

executes only after the animation for

playButtonContainer.gotoAndPlay(2); 

finishes executing and

playButtonContainer.gotoAndPlay(2); 

executes only after the

playButtonContainer.gotoAndPlay(2); 

animation is finished?

Upvotes: 0

Views: 1241

Answers (2)

gabriel
gabriel

Reputation: 2359

You could try something like:

playButtonContainer.addEventListener(MouseEvent.CLICK, playButtonClickedHandler, false, 0, true);

function playButtonClickedHandler(event:MouseEvent):void
{
    trace('instructionsContainer');
    instructionsContainer.addFrameScript(instructionsContainer.totalFrames -1, playStatusDescriptor);
    instructionsContainer.gotoAndPlay(2);
}

function playStatusDescriptor():void
{
    instructionsContainer.stop();
    trace('statusDescriptionContainer');
    statusDescriptionContainer.addFrameScript(statusDescriptionContainer.totalFrames -1, playAnalysisScreen);
    statusDescriptionContainer.gotoAndPlay(2);
}

function playAnalysisScreen():void
{
    statusDescriptionContainer.stop();
    trace('playAnalysisScreen');
    analysisScreenContainer.addFrameScript(analysisScreenContainer.totalFrames -1, playAnotherButton);
    analysisScreenContainer.gotoAndPlay(2);
}

function playAnotherButton():void
{
    analysisScreenContainer.stop();
    trace('playAnotherButton');
    anotherScreenContainer.addFrameScript(anotherScreenContainer.totalFrames -1, somethingElse);
    anotherScreenContainer.gotoAndPlay(2);
}

function somethingElse():void
{
    anotherScreenContainer.stop();
    trace('somethingElse');
}

Upvotes: 1

Fygo
Fygo

Reputation: 4665

What Cherniv suggested you or you could add an event listener and just listen to enter frame. There you just check whether the currentFrame equals the totalFrames. Or you could extend movieclip:

package {

    import flash.display.MovieClip;
    import flash.events.Event;

    public class AdvancedMovieClip extends MovieClip {

        public function AdvancedMovieClip() {
            this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        private function onEnterFrame(e:Event):void {
            if(this.currentFrame == this.totalFrames) {
                dispatchEvent(new Event(Event.COMPLETE));
            }
        }

        public function destroy():void {
            this.stop();
            this.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
    }
}

Upvotes: 1

Related Questions