13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Display programming<br />

container.removeChild(ball_C);<br />

trace(container.getChildAt(0).name); // ball_A<br />

trace(container.getChildAt(1).name); // ball_B<br />

The removeChild() and removeChildAt() methods do not delete a display object instance <strong>en</strong>tirely. They simply<br />

remove it from the child list of the container. The instance can still be refer<strong>en</strong>ced by another variable. (Use the delete<br />

operator to completely remove an object.)<br />

Because a display object has only one par<strong>en</strong>t container, you can add an instance of a display object to only one display<br />

object container. For example, the following code shows that the display object tf1 can exist in only one container (in<br />

this case, a Sprite, which ext<strong>en</strong>ds the DisplayObjectContainer class):<br />

tf1:TextField = new TextField();<br />

tf2:TextField = new TextField();<br />

tf1.name = "text 1";<br />

tf2.name = "text 2";<br />

container1:Sprite = new Sprite();<br />

container2:Sprite = new Sprite();<br />

container1.addChild(tf1);<br />

container1.addChild(tf2);<br />

container2.addChild(tf1);<br />

trace(container1.numChildr<strong>en</strong>); // 1<br />

trace(container1.getChildAt(0).name); // text 2<br />

trace(container2.numChildr<strong>en</strong>); // 1<br />

trace(container2.getChildAt(0).name); // text 1<br />

If you add a display object that is contained in one display object container to another display object container, it is<br />

removed from the first display object container’s child list.<br />

In addition to the methods described above, the DisplayObjectContainer class defines several methods for working<br />

with child display objects, including the following:<br />

contains(): Determines whether a display object is a child of a DisplayObjectContainer.<br />

getChildByName(): Retrieves a display object by name.<br />

getChildIndex(): Returns the index position of a display object.<br />

setChildIndex(): Changes the position of a child display object.<br />

removeChildr<strong>en</strong>(): Removes multiple child display objects.<br />

swapChildr<strong>en</strong>(): Swaps the front-to-back order of two display objects.<br />

swapChildr<strong>en</strong>At(): Swaps the front-to-back order of two display objects, specified by their index values.<br />

For more information, see the relevant <strong>en</strong>tries in the ActionScript 3.0 Refer<strong>en</strong>ce for the Adobe Flash Platform.<br />

Recall that a display object that is off the display list—one that is not included in a display object container that is a<br />

child of the Stage—is known as an off-list display object.<br />

Last updated 6/6/2012<br />

162

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!