Control Main Timeline from External swf

Control Main Timeline from External swf

Flash CS4 ActionScript 3.0 Tutorial

 

How to Control the Main Timeline from an External Flash Movie

 

 

Main timeline control can come from a button on the main stage, a button in a nested movie or a button in an external swf file. In this example we have a button located inside 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 text to the Content layer, frame 2.

Step Five

Save and name your fla file, Main.fla. 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

In the External.fla file, add a button with the instance name of external_btn. For more info on creating buttons, see topics, Button Symbol Basics and Instance Naming.

Step Eight

Add a new layer to the External file named Actions.

Step Nine

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

Step Ten

Copy/Paste the following code to the ActionScript panel.

external_btn.addEventListener(MouseEvent.CLICK, onReturn);

function onReturn(e:MouseEvent):void{

MovieClip(parent.parent).gotoAndStop(2);

}

Step Eleven

Save your file and publish your movie by pressing Cmd/Return (PC - Ctrl/Enter) then close the External.fla file. Don't worry if you see an error if you click the button at this step. It goes away once the file is opened from the Main file.

Step Twelve

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

Step Thirteen

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

Step Fourteen

Copy/Paste the following code in the ActionScript panel at frame one of the Actions layer.

stop();

var loadExt:Loader=new Loader;

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

addChildAt(loadExt,0);

Step Fifteen

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

Step Sixteen

Copy/Paste the following code in the ActionScript panel at frame two of the Actions layer.

removeChildAt(0);

Step Seventeen

Test your movie by pressing Cmd/Return (PC - Ctrl/Enter). The swf file with the external button appears. Click the button and you are sent to frame two of the main timeline.

Dissecting the Code

The first line of the code at Step Ten adds an event listener to the external_btn that is inside the External.swf file. Once it hears a mouse click, the function onReturn is run. The onReturn function says, "go to the the main timeline and stop at frame two".

The code at Step Fourteen 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 Sixteen code 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). For more information about adding and removing children see topic, Display List.

 

Royalty-Free Stock Video at Pond5