O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Containment Events

Earlier we learned how to use the addChild( ) and addChildAt( ) methods to add a new display child to a DisplayObjectContainer object. Recall the general code:

// The addChild() method
someContainer.addChild(newChild)

// The addChildAt() method
someContainer.addChild(newChild, depth)

We also learned that existing child display objects can be removed from a DisplayObjectContainer object via the removeChild( ) and removeChildAt( ) methods. Again, recall the following general code:

// The removeChild() method
someContainer.removeChild(childToRemove)
// The removeChildAt() method
someContainer.removeChildAt(depthOfChildToRemove)

Finally, we learned that an existing child display object can be removed from a container by moving that child to another container via either addChild( ) and addChildAt( ). Here's the code:

// Add child to someContainer
someContainer.addChild(child)

// Remove child from someContainer by moving it to someOtherContainer
someOtherContainer.addChild(child)

Each of these child additions and removals is accompanied by a built-in Flash runtime event—either Event.ADDED or Event.REMOVED. The following three sections explore how these two events are used in display programming.

Tip

The following sections require a good understanding of ActionScript's hierarchical event dispatch system, as discussed in Chapter 21. If you are not yet thoroughly familiar with hierarchical event dispatch, read Chapter 21 before continuing with the following sections.

The Event.ADDED ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required