Control an External File from the Main Timeline

Control an External File from the Main Timeline

Flash CS4 ActionScript 3.0 Tutorial

 

How to Control an External Timeline with Flash ActionScript on the Main Timeline

 

 

External movie timeline control can come from a button on the main stage or a button in an external swf file. In this example we have a button located on the stage of the main timeline that controls the playhead of the external timeline.

Tutorial Elements

The external .fla file and .swf file are placed in a folder named Swf. The external files should be named External.swf and External.fla. The Swf folder should be at the same level as the host swf file (Main.swf).

A button on the stage of the main timeline named external_btn.

 

Step One

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

Step Two

Add a new top layer and name it Actions.

Step Three

Click frame one of the Button layer and add a button symbol with instance name external_btn.

Step Four

Save your fla file, name it Main.fla, and create a folder called Swf at the same level in the directory.

Step Five

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

Step Six

In the External.fla file, click frame two and add a keyframe by pressing F6.

Step Seven

Add some Static Text to frame one and frame two. For more info on creating text, see topic, Create Text. By scrubbing the timeline you should see the content in your two frames.

Step Eight

Add a new top layer called Actions.

Step Nine

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

Step Ten

Copy/Paste the following code to the ActionScript panel.

stop();

Step Eleven

Save your External.fla file and test your movie by pressing Cmd/Return (PC - Ctrl/Enter). It should open and stop at frame one.

Step Twelve

Return to your Main.fla file and Option double-click (PC - Alt double-click) frame one of the Actions layer.

Step Thirteen

Copy/Paste the following code to the ActionScript panel.

stop();

var loadExt:Loader=new Loader;

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

addChild(loadExt);

external_btn.addEventListener(MouseEvent.CLICK, onAdvance);

function onAdvance(e:MouseEvent):void{

var extTimeline:MovieClip=loadExt.content as MovieClip;

extTimeline.gotoAndStop(2);

}

Step Fourteen

Publish your movie by pressing Cmd/Return (PC - Ctrl/Enter). The external movie loads. When you press the button, the external movie's timeline jumps to frame two.

Dissecting the Code

The first two lines of the code at Step Thirteen introduce a loader named loadExt and grab the External.swf file in the Swf folder. The addChild line places the external file on the stage.

The next few lines add an event listener to the external_btn. Once it hears a mouse click, the function onAdvance is run. The onAdvance function introduces a new MovieClip variable named extTimeline.

The final line sends the External.swf timeline to frame two.

 

Stock Photos from 123RF