Removing an External File with While Statement

Removing an External File with While Statement

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.

In this example, we will demonstrate the first method by checking the Display List to determine how many children are on the stage. Any extras are removed from the bottom of the stack, one at a time, with the While statement. For more information on the Display List and While statements, see topics, Display List and While Statement.

Tutorial Elements

External swf file with instance name External.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

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();

while(this.numChildren>3){

this.removeChildAt(0);

}

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"));

addChildAt(loader01,0);

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 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. The next section of code is the while statement. It checks to see if there are more than three children on the stage. Each of the buttons counts as a child and the text block is the third. If there are more than three children, it removes the child at the bottom of the stack.

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 three adds loader01 to the stage at the bottom of the Display List. It is important to add the loader contents to the bottom of the list because the while statement at frame 2 removes children from the bottom up.

 

Shutterfly 50 Free Prints 160x600