Controlling Main Timeline from External Movie

Controlling Main Timeline from External Movie

Flash CS4 ActionScript 3.0 Tutorial

 

How to Control Main Timeline from External Movie with Flash ActionScript

 

 

Main timeline control can come from ActionScript on the main timeline, a nested movie's timeline or aa external swf file timeline. In this example we have a ActionScript located on the timeline of an external movie that controls the playhead of the main timeline. The external file is introduced to the main timeline with the Loader class.

Tutorial Elements

External .fla file and .swf file in folder named Swf. The external files should be named external.swf and external.fla. Swf folder should be at the same level as the host swf file.

 

Step One

Open a new Flash document and name the default layer Content.

Step Two

Add a new top layer and name it Actions.

Step Three

Click frame two of the Content layer and press F6 to add a keyframe.

Step Four

Using the Text tool add some Static text to the Content layer, frame 2.

Step Five

Save your fla file in a Project folder and create a folder named Swf at the same level in the directory.

Step Six

Open a new flash document named External.fla and save it in the Swf folder (File > New)

Step Seven

Name the default layer Contents, the add a new layer to the External file called Actions.

Step Eight

Select frame 18 of the Actions layer and press F6 to add a new keyframe at frame 18.

Step Nine

Option double-click (PC - Alt double-click) frame 18 of the Actions layer in the External file.

Step Ten

Copy/Paste the following code to the ActionScript panel.

stop();

MovieClip(parent.parent).play();

Step Eleven

Add a static text object with the Text tool to the Content layer.

Step Twelve

Select frame 18 of the Content layer, then press F5 to extend it's content to frame 18.

Step Thirteen

Publish your movie by pressing Cmd/Return (PC - Ctrl/Enter) then save and close the External.fla file. Don't be alarmed if you see an error. Since you are playing it without a parent an error occurs.

Step Fourteen

Returning to the original host .fla file, add a keyframe to the Actions layer at frame two by selecting frame two then press F6. This layer should have two keyframes, one at frame one and one at frame two.

Step Fifteen

Option double-click (PC - Alt double-click) frame one of the Actions layer.

Step Sixteen

Copy/Paste the following code in the ActionScript panel.

stop();

var loadExt:Loader=new Loader;

loadExt.load(new URLRequest("Swf/External.swf"));

addChildAt(loadExt,0);

Step Seventeen

Option double-click (PC - Alt double-click) frame two of the Actions layer.

Step Eighteen

Copy/Paste the following code in the ActionScript panel.

stop();

removeChildAt(0);

Step Nineteen

Test your movie by pressing Cmd/Return (PC - Ctrl/Enter). The swf file with the external text appears. Once the external file reaches frame 18, the main timeline advances to frame two and the external movie is removed from the display list.

Dissecting the Code

The first line of the code at Step Ten stops the main timeline of the external file. The second line instructs the parent's parent (the main timeline) to play.

The code at Step Sixteen stops the main timeline at frame one. A new variable of the loader class named loadExt is introduced. The loadExt variable is loaded with the External.swf file. Once it is loaded, it is added to the stage at the bottom level.

The Step Eighteen code stops the main timeline and removes the external swf file. Since we added the file to the bottom of the stack with the addChildAt(loadExt,0) command, it is easy to remove the content from the lowest level by saying removeChildAt(0).

 

Contribute Ad