Control External File with a Button on External Timeline

Control External File with a Button on External Timeline

Flash CS4 ActionScript 3.0 Tutorial

 

How to Control External Timeline with a Button in External Flash File

 

 

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 external file timeline that controls the playhead of the external timeline.

An external file is one that is introduced to the main timeline using the Loader class. The external .swf file is located in a Swf folder that resides next to the main .swf file timelline.

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 external file timeline named external_btn.

 

Step One

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

Step Two

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

Step Three

Copy/Paste the following code to the ActionScript panel.

var loadExt:Loader=new Loader;

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

addChild(loadExt);

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 one 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

Name the default layer, Text.

Step Nine

Add a new top layer and name it Button.

Step Ten

Add a Buttton Symbol to the Button layer and assign it an instance name of external_btn in the Properties panel. For more information on creating Button Symbols, see topic, Button Symbol Basics.

Step Eleven

Add a new top layer called Actions to the External.fla file.

Step Twelve

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

Step Thirteen

Copy/Paste the following code to the ActionScript panel.

stop();

external_btn.addEventListener(MouseEvent.CLICK, onAdvance);

function onAdvance(e:MouseEvent):void{

gotoAndStop(2);

}

Step Fourteen

Save your External.fla file and test your movie by pressing Cmd/Return (PC - Ctrl/Enter). It should open and stop at frame one. By pressing the button it advances to frame two.

Step Fifteen

Return to your Main.fla file and 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 Three 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.

At Step Thirteen, the code stops the timeline of the external file at frame one. The next section adds an event listener to the external_btn. Once it hears a mouse click, the function onAdvance is run. The onAdvance function sends the timeline to frame two of the External.swf file.

 

Stock Images