Loading an SWF whose Code is in a Document Class

Here we discuss the case when the loaded SWF contains references to 'stage' and its code resides in an external Document Class. The new issue arises if references to 'stage' appear in the constructor of the loaded SWF's Document Class. That is because the loaded SWF is instantiated before it is added as a child of 'loader'. Thus, when the constructor runs the instance is not on the Display List even if 'loader' is.

To fix this problem, a listener to Event.ADDED_TO_STAGE has to be added to the constructor. We show below how to do it.

In this part of the experiment, we have container_2.fla whose code is the same as in container_1.fla on the previous page except for the comments and for the 'loader' loading the file 'ext_docclass.swf' instead of 'ext_timline.swf'. The functionality of the resulting applet does not change as the functionality of 'ext_docclass.swf' is the same. The difference is in the code for 'ext_docclass.swf' which now resides in an external class 'ThreeCircles.as'. ThreeCircles is assigned as the Document Class in ext_docclass.fla.

Here are the relevant portions of the Document Class, ThreeCircles.as.

 

package {

import flash.display.*;

import flash.events.*;

import flash.text.*;

import flash.filters.*;

public class ThreeCircles extends MovieClip {

var circle1:Shape;

var circle2:Shape;

var circle3:Shape;

//Beginning of the constructor.

public function ThreeCircles(){

setUpCircles();

renderCircles();

 

//The next commented out line would cause an error

//when ext_docclass.swf is loaded at runtime.

 

//stage.addEventListener(MouseEvent.CLICK,whenClicked);

 

//To deal with the issue, we add a listener to ADDED_TO_STAGE event.

//The event is fired and targeted at a child when the child

//(here an instance of ThreeCicles) is added to the Display List.

//The first part of the conditional runs when ext_docclass.fla is compiled.

//The second part runs when ext_docclass.swf is loaded.

 

if(stage==null){

this.addEventListener(Event.ADDED_TO_STAGE, addedToList);

} else {

stage.addEventListener(MouseEvent.CLICK,whenClicked);

}

}

//End of the constructor.

 

private function addedToList(e:Event):void {

stage.addEventListener(MouseEvent.CLICK,whenClicked);

}

 

private function whenClicked(e:MouseEvent):void {

renderCircles();

}

 

//Then there come methods that set up and draw circles.

 

private function setUpCircles():void {

...........................

}

 

private function renderCircles():void {

...........................

}

 

public function renderOrangeCircles():void {

...........................

}

...........................

}

}

Download

Download all the 'fla' and 'as' files corresponding to the isues discussed in this tutorial in one zip file.

Another important question concerns unloading of SWFs loaded at runtime. The Loader.unload() method available in Player 9 merely removes the loaded content from the Display List. It does not clear it from memory. The best you can do is remove all references to the loaded content and hope that the garbage collector will eventually remove the loaded swf from memory. This is more difficult in the case of SWFs that assign listeners to 'stage' as those listeners remain even after the content is unloaded. We discuss the issue of unloading on the next page.

Back to Intermediate Tutorials              Back to Flash and Math Home

We welcome your comments, suggestions, and contributions. Click the Contact Us link below and email one of us.

Adobe®, Flash®, ActionScript®, Flex® are registered trademarks of Adobe Systems Incorporated.