AS2 to AS3: get all objects in a movieclip

Sometimes you want a list of everything inside a movieclip. For example: you want to know the instance names of every movie in the root.

ActionScript 2

A little trick that I used a lot in AS2 is:

for(var i in _root){
   trace('key: ' + i + ', value: ' + _root[i]);
}

or

for(var i in target_mc){
   trace('key: ' + i + ', value: ' + target_mc[i]);
}

to find out which movies are in target_mc

It was not only a trick to trace everything, I also used it to reset movieclips (place them outside the stage, or delete them) or quickly make buttons out of them:

for(var i in target_mc){
	target_mc[i].id = i;
	target_mc[i].onRelease = function (){
		trace ("id = " + this.id); // onRelease the id will be called (the name of the movie)
	};
}

ActionScript 3

But in AS3 this doesn’t work any more!
And I know: AS3 should make my life easier…. well if I see the solution to the problem created by AS3, I have to disagree!

for (var i:uint = 0; i < target_mc.numChildren; i++){
	trace ('\t|\t ' +i+'.\t name:' + target_mc.getChildAt(i).name + '\t type:' + typeof (target_mc.getChildAt(i))+ '\t' + target_mc.getChildAt(i));
}

In AS3 you first have to get the "number of children" (numChildren) in a DisplayObjectContainer, then you have to say which child you want (getChildAt(i))...
A good thing about AS3 is, you get everything in a movieClip, even shapes you made there without a instance name.

I'm glad that I work with FlashDevelop, which has snippets so I don't have to type this constantly!

(For the people that use FlashDevelop too, here is my snippet:)

// $(Clipboard)is a DisplayObject
trace ('+ number of DisplayObject: ' + $(Clipboard).numChildren + '  --------------------------------');
for (var i:uint = 0; i < $(Clipboard).numChildren; i++){
	trace ('\t|\t ' +i+'.\t name:' + $(Clipboard).getChildAt(i).name + '\t type:' + typeof ($(Clipboard).getChildAt(i))+ '\t' + $(Clipboard).getChildAt(i));
}
trace ('\t+ --------------------------------------------------------------------------------------');
  • del.icio.us
  • NewsVine
  • Reddit
  • StumbleUpon
  • Technorati
  • Digg
  • email
  • Hyves
  • Facebook
  • Google Bookmarks
  • MySpace
  • LinkedIn
  • Twitter

Related posts

10 Comments

  1. Posted May 30, 2008 at 11:36 am | Permalink

    what about all the functions and objects data you could get out of a movieclip next to all the visual stuff ? in as2 you can get [type Function] and [object BlurFilter]

  2. Posted August 15, 2008 at 4:16 pm | Permalink

    Thanks so much, this is great help. AS3’s way of adding and removing children really is hard to grasp when you’re first learning, but this makes things so much easier.

  3. Posted November 16, 2008 at 2:33 am | Permalink

    Here’s a little addition to your function:

    var target_mc = this;
    for (var i:uint = 0; i < target_mc.numChildren; i++){
    trace (‘\t|\t ‘ +i+’.\t name:’ + target_mc.getChildAt(i).name + ‘\t type:’ + typeof (target_mc.getChildAt(i))+ ‘\t’ + target_mc.getChildAt(i));
    if(target_mc.getChildAt(i).hasOwnProperty(“numChildren”)) {
    trace(‘\t\t\t\t children:’);
    for(var j:uint = 0; j , target_mc.getChildAt(i).numChildren; i++) {
    trace(‘\t|\t\t\t\t\t’ +j+’.\t name:’ + target_mc.getChildAt(i).getChildAt(j).name + ‘\t type:’ + typeof (target_mc.getChildAt(i).getChildAt(j))+ ‘\t’ + target_mc.getChildAt(i).getChildAt(j));
    }
    }
    }

    This just takes it a step further and checks if target_mc’s children have any children, then lists all the children of target_mc’s children. It would be redundant to go any deeper into the display lists b/c you can change just target_mc once you know that target_mc’s children have children. (little confusing, but useful heh)

  4. Luke
    Posted January 15, 2009 at 8:57 am | Permalink

    correct me if im wrong but :
    this does not get all objects in a movie clip. It only gets what objects are on the display list. what about those objects that arnt ?

  5. Posted January 16, 2009 at 10:00 pm | Permalink

    Thanks so much for this post – its the only one I found that shows how to do this.
    JD:
    In your code, you need to test for type of object, as not all objects can have children (at least that was my experience).
    Try this inside the original loop:
    if (MovieClip(root).getChildAt(i) is MovieClip)
    {
    trace(“It’s a MC”);
    tmpObj = MovieClip(root).getChildAt(i);
    if(tmpObj.hasOwnProperty(“numChildren”))
    {
    trace (tmpObj.name+” has “+tmpObj.numChildren+” children:”);
    for(j = 0; j < tmpObj.numChildren; j++)
    {
    trace (“\t”+tmpObj.getChildAt(j).name+” is of type: “+typeof ( tmpObj.getChildAt(j) ) );
    }
    }
    else trace (tmpObj.name+” has no children.”);
    }

  6. Posted April 7, 2009 at 12:55 pm | Permalink

    Thanks a ton, it was time saver.
    I had made a menu which had a header with bg and corresponding list to the header with bg, so it involved TextField etc.

    Looping them got me the first set of children but failed to check whether the child had children. Any way I tweaked your code.

    thanks a ton again.

  7. Posted April 7, 2009 at 3:56 pm | Permalink

    perhaps you can share your code with us/me?

  8. Posted June 1, 2009 at 3:27 pm | Permalink

    Yo MCK, you saved me a long dreary search through the as3 documentation. Thnx dude!

  9. Fin
    Posted June 2, 2009 at 11:33 pm | Permalink

    Cheers. That’s awesome.

  10. Prasanna
    Posted October 6, 2009 at 6:20 am | Permalink

    Rectified one of Jonathan Dumaine::

    var target_mc = this;
    for (var i:uint = 0; i < target_mc.numChildren; i++) {
    trace('\t|\t ' +i+'.\t name:' + target_mc.getChildAt(i).name + '\t type:' + typeof (target_mc.getChildAt(i))+ '\t' + target_mc.getChildAt(i));
    if (target_mc.getChildAt(i).hasOwnProperty("numChildren")) {
    trace('\t\t\t\t children:');
    for (var j:uint = 0; j < target_mc.getChildAt(i).numChildren; j++) {
    trace('\t|\t\t\t\t\t' +j+'.\t name:' + target_mc.getChildAt(i).getChildAt(j).name + '\t type:' + typeof (target_mc.getChildAt(i).getChildAt(j))+ '\t' + target_mc.getChildAt(i).getChildAt(j));
    }
    }
    }

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*