Adding an External File to Specific Level of Display List
Flash CS4 ActionScript 3.0 Tutorial
How to Place an Object at a Specific Level of the Display List in Flash
The Display List plays an important role in your Flash files. It keeps track of the all the objects on the stage at any given time. You can import external files or files from the Library and specify that they be added to the top, bottom or at a specific level.
It is easy to confuse the levels of the Display List and the layers in your Flash document. As you add objects to your layers, they are added to the Display List. It is possible to add external files at specific levels of the Display List or remove an object at a particular level.
In this example we will add an external file to a specific level of the Display List using the addChildAt() statement.
For more information on Display Lists, see topics, Adding an External File to the Bottom of the Display List, Adding an External File to the Top of the Display List, Checking the Display List Contents and Removing a File from the Display List.
An external swf file named External.swf located in a folder named Swf. The folder should be at the same level at the host swf file.
Open a new Flash document and add a new layer.
Name the upper layer Actions and the lower layer Text. Select frame one of the Text layer.
Using the Text tool, add the numbers 0,1 and 2 to the stage. The numbers should be added as a single text block.
With the text selected, choose Modify > Break Apart. This converts the text object into three separate text objects.
Save your document and name it Main.fla. Add a new folder named Swf at the same level as your Main.fla file.
Open a new document (File > New) named External.fla and save it in the Swf folder.
Using the Rectangle tool, add a rectangle to the stage.
Publish your External movie by pressing Cmd/Return (PC - Ctrl/Enter).
Returning to the Main.fla file, Option double-click (PC - Alt double-click) frame one of the Actions layer.
Copy/Paste the following code to the ActionScript panel.
var loader01:Loader = new Loader();
Publish your movie by pressing Cmd/Return (PC - Ctrl/Enter). The external file is added to the stage between the numbers one and two.
Dissecting the Code
The code at Step Ten adds a new Loader variable named loader01. The next line fills the loader with the External.swf file.
The final line adds the loader01 to the stage. By using the addChildAt() method and specifying the level to be one, the external file is added between two of the numbers. The lowest level in the display list is level 0.
Before the external file was added, the numbers occupied level 0, level 1 and level 2. Once the external file was added, the two was on level 0, the external file (rectangle) occupied level 1, and levels 2 and 3 contained the other two numbers (zero and one).
In this example, acceptable values for the addChildAt method are 0, 1, 2 or 3. Entering any other value would return an error.