Removing an External File by Unloading swf File

Removing an External File by Unloading swf File

Flash CS4 ActionScript 3.0 Tutorial

 

How to Remove an External Flash File with 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. In this example, we will demonstrate the second method by unloading the file once it is no longer needed.

Tutorial Elements

External swf file with instance name External.swf.

Two buttons with instance names of button01_btn and button02_btn. The two buttons should be labeled Frame 02 and Frame 10 respectively.

 

Step One

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

Step Two

Name the upper layer Actions and the lower ones Buttons 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 two buttons to the Buttons layer and assign them instance names of button01_btn and button02_btn in the Properties panel.

Step Nine

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

Step Ten

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

Step Eleven

Copy/Paste the following code to the ActionScript panel.

var loader01:Loader = new Loader();

Step Twelve

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

Step Thirteen

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

Step Fourteen

Copy/Paste the following code to the ActionScript panel.

stop();

loader01.unload();

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 Fifteen

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

Step Sixteen

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

Step Seventeen

Copy/Paste the following code to the ActionScript panel.

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

addChild(loader01);

Step Eighteen

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 you are returned to frame two and the external file is removed.

Dissecting the Code

The first line of the code at Step Eleven 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 Fourteen stops the main timeline. Line two unloads any content in the loader. This is helpful if the timeline goes to frame 10, then back to frame 2. Once it returns to frame 2, the frame 10 content is removed.

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 Seventeen makes a URLRequest to import the external file. Line two adds loader01 to the stage.

Removing a Child by Name

To remove a child by name you would use the removeChild() method. For instance, if you had an instance on the stage named, greenBox_mc, you would use the following code:

removeChild(greenBox_mc);

Remove verses Unload

In this example we used the unload() method. Unloading an object keeps the loader in the Display List while it's content is removed.

If you wanted to keep the contents in the loader and remove the loader from the display list, you would use the removeChild() or removeChildAt() method.

To remove a loader called ldr01 from the Display List you would use the following code:

removeChild(ldr01);

For more information on removing children from the Display List, see topic, Removing a File from the Display List.

 

Stock Photos from 123RF