PDA

View Full Version : how to use addChilds return value. addChild trick! you'll like this one


GFX Complex
08-14-2008, 02:26 AM
ok so I have never seen any one ever show this. I know it's not a huge deal or any thing but I think it makes for some really nice short hand code and kills two birds with one stone.

the idea is to use the return type of addChild which is the target argument that is passed to addChild.

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

so why not addChild and push in the same line?

var _mc:MovieClip = new MovieClip();
var array:Array = new Array();

array.push(addChild(_mc));
trace(array);// outputs [object MovieClip]

I have made a post about this in my AS3 user groups blog, but I though you guys here would like to see this too.

here is a link to the blog if you want it.
http://www.as3apex.com/as3/cool-addchild-trick-why-dont-people-not-use-the-return-of-addchild/