Tag Archive for 'DisplayObject'

Cool addChild trick! Why don’t people use the return of addChild?

I was doing some home work on the addChild method and found that it's has a return type of DisplayObject. At first I thought how strange that is since  most people would never think to use addChild as a way to get a return value.  And that's when I had an idea. Why not use the return of addChild?

A very common thing to do when you add a DisplayObject to the DisplayList is to also save that same DisplayObject in a Array so that you have fast access to that DisplayObject if you need to use removeChild or what have you.

so why not addChild and push in the same line?

Actionscript:
  1. var _mc:MovieClip = new MovieClip();
  2. var array:Array = new Array();
  3.  
  4. array.push(addChild(_mc)); // addChild with push
  5. trace(array);//outputs [object MovieClip]

I know this is really simple and not a huge deal, but since I have never seen any one else use addChild in such a way I thought it would be cool to see what every one else thinks.