flash display_list_container_reallocation

Container reallocation

In ActionScript 1 and 2, no method existed to move a visual object in the display list. It was impossible to change its parent. The only way was to delete the visual object then to recreate it in the desired container.

In ActionScript 3, if we put a DisplayObject that is already in the display list in the addChild method it is deleted from its original container and placed in the new DisplayObjectContainer on which we have called the addChild method.

Take a simple example, we create a clip that we add to a display list by adding a new main timeline:

var myClip:MovieClip = new MovieClip();
// the myClip clip is added to the main timeline
addChild ( myClip );
// display : 1
trace( numChildren );

The numChildren property of the main timeline sends back 1 as the myClip clip is the child of the main timeline.

We now create a Sprite type visual object and we add our myClip clip as a child to it already contained in the main timeline:

var myClip:MovieClip = new MovieClip();
// the myClip clip is added to the main timeline
addChild ( myClip );
// display : 1
trace( numChildren );
var mySprite:Sprite = new Sprite();
addChild ( mySprite );
// display : 2
trace( numChildren );
mySprite.addChild ( myClip );
// display : 1
trace( numChildren );

The numChildren property of the main timeline still sends back 1 as the myClip clip has left the main timeline in order to be placed in our mySprite object.

In the following code, the successive calling of the addChild method doesn’t duplicate the visual object but moves it from its original container to replace it again:

var myClip:MovieClip = new MovieClip();
// the myClip clip is added to the main timeline
addChild ( myClip );
// the myClip clip leaves the main timeline
// then is replaced in it
addChild ( myClip );
// display : 1
trace( numChildren );

There is no equivalent in ActionScript 3 to the existing duplicateMovieClip method. To duplicate a visual object we use the keyword new.

The reallocation of containers shows the power brought by Flash 9 player and ActionScript 3 in regard to the management of visual objects.








Adobe Training center


Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42