Checking the Display List Contents

Checking the Display List Contents

Flash CS4 ActionScript 3.0 Tutorial

 

How to Determine the Number of Objects in the Display List with Flash ActionScript

 

 

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 query the total number of objects in the Display List and specific object names.

For more information on Display Lists, see topics, Adding a File to a Specific Level of a Display List, Adding an External File to the Bottom of the Display List, Adding an External File to the Top of the Display List and Removing a File from the Display List.

Tutorial Elements

A Button Symbol

 

Step One

Open a new Flash document and add a new layer.

Step Two

Name the upper layer Actions and the lower layer Content. Select frame one of the Content layer.

Step Three

Add a Button Symbol to the stage on the Content layer.

Step Four

With the Button Symbol selected, assigned it an instance name of button01_btn in the Properties panel.

Step Five

Option double-click (PC - Alt double-click) frame one in the Actions layer to open the ActionScript panel.

Step Six

Copy/Paste the following code to the ActionScript panel.

trace(numChildren);

trace(getChildAt(0));

trace(getChildIndex(button01_btn));

trace(getChildByName("button01_btn"));

Step Seven

Publish your movie by pressing Cmd/Return (PC - Ctrl/Enter). The Output panel displays the total number of levels (children) in the Display List. It also shows that the child on level 0 is a SimpleButton. The next trace statement indicates that the button is on level 0. The final trace statement shows that the instance named button01_btn is an instance of a SimpleButton.

Dissecting the Code

The code at Step Six traces several aspects of the display object. The first trace statement determines that there is only one object on the stage. The next trace statement shows that the object on level 0 is a SimpleButton.

Trace statement number three returns the value of zero for the location of the button01_btn. The final trace statement displays that the button01_btn instance is a SimpleButton.

Depending on how you create your button, it may be either a MovieClip or a SimpleButton.