Removing an External File with a Blank swf File

Removing an External File with a Blank swf File

Flash CS4 ActionScript 3.0 Tutorial

 

How to Remove an External FileĀ Using 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. In this example, we will demonstrate the second method.

Tutorial Elements

External swf file with instance name External.swf

External swf file with instance name of Blank.swf

Two buttons with instance names of button01_btn and button02_btn

 

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

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

Step Seven

Publish the Blank file (Cmd/Return (PC - Ctrl/Enter).

Step Eight

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

Step Nine

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

Step Ten

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

Step Eleven

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

Step Twelve

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

Step Thirteen

Copy/Paste the following code to the ActionScript panel.

var loader01:Loader = new Loader();

Step Fourteen

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

Step Fifteen

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

Step Sixteen

Copy/Paste the following code to the ActionScript panel.

stop();

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

addChild(loader01);

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 Seventeen

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

Step Eighteen

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

Step Nineteen

Copy/Paste the following code to the ActionScript panel.

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

addChild(loader01);

Step Twenty

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 and the Blank.swf is loaded in it's place.

Dissecting the Code

The first line of the code at Step Thirteen 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 Sixteen stops the main timeline. Line two makes a URLRequest to import the blank external file. Line three adds loader01 to the stage.

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 Nineteen makes a URLRequest to import the external file. Line three adds loader01 to the stage. By adding the new external file, the blank file is removed. The same is true when the user returns to frame 2. By returning to frame 2, the external file is removed and the blank file is loaded.

 

Royalty-Free Stock Video at Pond5