Removing an External File from a Movie Symbol

Removing an External File from a Movie Symbol

Flash CS4 ActionScript 3.0 Tutorial

 

How to Remove an External File with Flash ActionScript

 

 

External files can be removed multiple ways. One method checks to see how many items are in the display list. If the number is too high, items are removed from the bottom of the list.

A second method takes advantage of the fact that a single loader object can only hold one external file at a time.

A third method can be used when an external file is loaded into a Movie Symbol on the stage. By making the Movie Symbol only visible at a certain frame, you can change frames and the external object disappears. In this example we will demonstrate the third method.

Tutorial Elements

External swf file with instance name External.swf.

Two buttons with instance names of button01_btn and button02_btn.

An empty Movie Symbol with instance name holder_mc.

 

Step One

Open a new Flash document and add a three additional layers to the Layers panel.

Step Two

Name the upper layer Actions and the lower ones Buttons, HolderMovie and Text.

Step Three

Save your file in a Project folder and name the file Main.fla.

Step Four

Open a new Flash document (File > New) and name it External.fla. Save it in the same Project folder. It should be set to the same size and frame rate as your Main.fla file.

Step Five

Add an animation or series of images to the External.fla file and publish the file (Cmd/Return (PC - Ctrl/Enter).

Step Six

Return to the Main.fla file and click the first frame of the Text layer. Add some text to the stage.

Step Seven

Click frame two of the Text layer and press F5 to extend the content to frame two.

Step Eight

Add a Movie Symbol to the Library by choosing Insert > New Symbol (Cmd/F8 (PC - Ctrl/F8)). Name the Movie Symbol, Holder. This adds a Movie Symbol to the Library without adding it to the stage.

Step Nine

Return to the main stage by clicking the Scene icon. Click frame 10 of the HolderMovie layer and add a keyframe (F6).

Step Ten

Drag your new symbol that you created in Step Eight from the Library to the upper left corner of the stage.

Step Eleven

Click the circle (Registration Point) of your new symbol and assign it an instance name of holder_mc in the Properties panel.

Step Twelve

Add two buttons to the Buttons layer and assign them instance names of button01_btn and button02_btn in the Properties panel.

Step Thirteen

Click frame ten of the Buttons layer and press F5 to extend the content to frame ten.

Step Fourteen

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

Step Fifteen

Copy/Paste the following code to the ActionScript panel.

var loader01:Loader = new Loader();

Step Sixteen

Click frame two of the Actions layer and press F6 to add a new keyframe at frame two.

Step Seventeen

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

Step Eighteen

Copy/Paste the following code to the ActionScript panel.

stop();

button01_btn.addEventListener(MouseEvent.CLICK, onClick01);

function onClick01(e:MouseEvent):void{

gotoAndStop(2);

}

button02_btn.addEventListener(MouseEvent.CLICK, onClick02);

function onClick02(e:MouseEvent):void{

gotoAndStop(10);

}

Step Nineteen

Click frame ten of the Actions layer and press F6 to add a keyframe.

Step Twenty

Option double-click (PC - Alt double-click) frame ten of the Actions layer to open the ActionScript panel.

Step Twenty One

Copy/Paste the following code to the ActionScript panel.

loader01.load(new URLRequest("External.swf"));

holder_mc.addChild(loader01);

Step Twenty Two

Publish your movie by pressing Cmd/Return (PC - Ctrl/Enter). The external file appears on the main stage when you press the Frame 10 button. By pressing the Frame 02 button, the external file is removed.

Dissecting the Code

The first line of the code at Step Fifteen establishes a new Loader variable called loader01. This code is at frame one to establish the new variable only once. Since the buttons never take you back to frame one, the loader is added only once.

The first line of code at Step Eighteen stops the main timeline.

The next several lines add functionality to the buttons. When you press one button it takes you to frame 2. The other button sends the main timeline to frame 10.

The first line of code at Step Twenty One makes a URLRequest to import the external file. Line two adds loader01 to the to the Movie Symbol instance named holder_mc.

You can reposition holder_mc on the stage manually by moving it's Registration Point or with ActionScript to change the location of the external movie.

 

Stock Images