Actionscript – .currentFrame in ActionScript 3

actionscriptactionscript-3flash

I asked before with a similar problem and received a great answer, so I apologise if this is a little frustrating. Hopefully it will be a quick one. I'm designing an interactive Flash tutorial in an effort to explain a complex biological problem, and I have structured the Timeline so that there are no more than two frames and four layers. I designed this before on the Stage timeline but it became so messy, and with a few problems, that I decided I had to redo it this way.

It consist of three main parts – A title that fades in first in two segments, followed by two buttons (that are disabled until they fully fade in) and finally animations of molecules that fade in and out indefinitely on a loop. The page then stays ambient until a user clicks one of the buttons.

I have four layers on the main timeline – Actions, Buttons, Molecules and Titles. In each are the relevant images and animations.

I want to code it so that each plays successively after the other, but I'm having real difficult accessing other timelines through AS3.

Currently I have this in the Actions layer:

import flash.events.Event;

NRPSText_mc.addEventListener(Event.ENTER_FRAME, FadeIn);

function FadeIn(event:Event):void
{
    if (MovieClip(this.root).currentFrame > 0) {
        NRPSText_mc.gotoAndPlay("NRPSFadeIn")
    }
}

ColourButton_mc.addEventListener(Event.ENTER_FRAME, BtnFadeIn);

function BtnFadeIn(event:Event):void
{
    if (NRPSText_mc.currentFrame == 30) {
        ColourButton_mc.gotoAndPlay("ButtonPress")
    }
}

It should be clear from this that I've labelled certain events in each timeline, and I want them to play as one finishes.

The problem is knowing what to put before ".currentFrame" in each instance, and I can't find it anywhere! Until now I have been able to get by using "this" and "MovieClip(this.root)", but I need to be able to find out how to references these embedded timelines to make it work. I've tried these codes with "trace" and it seems to work fine, so I assume this is where the problem lies.

Best Solution

First of all if you have code on main timeline instead of MovieClip(this.root).currentFrame you can just use currentFrame since you are refering to the object you set the script on.

Second, this

if (MovieClip(this.root).currentFrame > 0) {
    NRPSText_mc.gotoAndPlay("NRPSFadeIn")
}

will just make NRPSText_mc stay at "NRPSFadeIn" label since called each frame.

Third, if you have a movieclip on stage and you have set its instance name you can reference its timeline FROM parent scope (main timeline) by that instance name as you do with NRPSText_mc.gotoAndPlay("NRPSFadeIn"). If you got a script INSIDE that movieclip just use gotoAndPlay() and currentFrame because you are in the same object as a script.

Also you are doing it a bit wrong. Common approach is to use timeline animation or some kind of a tweening library like TweenMax. As you are using Flash IDE the proper way of doing same thing would be the following: create separate movieclips with whatever animation for your objects you want; place them on main timeline which got its own animation involving these clips; on whatever frame you want to control your inner clips set separate scripts with stop(), gotoAndPlay(), etc.

If you want to be notified when a clip finished playing use AS3 events. For example you got a movieclip named mc, add a script to the last frame dispatchEvent(new Event("stopped!")); stop(); and at main timeline

mc.addEventListener("stopped!", listener);
function listener(event:Event):void {
  trace("mc stopped!");
}

This way it is much easier than checking for frame numbers every enterframe event.

Related Question