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+ --------------------------------------------------------------------------------------');

    20 thoughts on “AS2 to AS3: get all objects in a movieclip

    1. 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. 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. 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(‘tttt children:’);
      for(var j:uint = 0; j , target_mc.getChildAt(i).numChildren; i++) {
      trace(‘t|ttttt’ +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. 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. 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. 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. 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));
      }
      }
      }

    8. go with the example of all the children of a movie clip I can access the movie clip properties perfectly but I want to access the properties that I have created a class of that MovieClip.
      with hasOwnProperty I can know if there is such a property but does not give me their value.
      I hope I can help

      Thanks before hand. from Venezuela

      using google translator

    9. not exactly sure what you mean, but you could cast a movieclip:

      // there is a movieclip with the instance name "test"
      var _mov:CustomMovieClipClass = this.getChildByName ("test") as CustomMovieClipClass;
      _mov.myAwesomeFunction (width, height);

    10. What if I want to manipulate one of those objects? I have a function that you pass a MovieClip to. But when I do this:

      myFunction(_root.getChildAt(2));

      I get an error:Implicit coercion of a value with static type
      flash.display:DisplayObject to a possibly unrelated type flash.display:MovieClip.

      Thanks!

    11. we all know how to get this in AS3

      below is to get it work in AS2 >

      var children:Array = [];

      function temp(mc){
      for (var p:String in mc) {
      if (getTypeOf(mc[p]) == “movieclip”){
      temp(mc[p]);
      }
      if (getTypeOf(mc[p]) == “textfield”){
      children.push(mc[p]);
      }
      }
      }

      temp(_mc);

      function getTypeOf(obj:Object):String {
      if (obj instanceof TextField)
      return ‘textfield’;
      var t:String = typeof obj;
      if (t != ‘object’)
      return t;
      if (obj instanceof Array)
      return ‘array’;
      if (obj instanceof Button)
      return ‘button’;
      if (obj instanceof Color)
      return ‘color’;
      if (obj instanceof ContextMenu)
      return ‘contextmenu’;
      if (obj instanceof ContextMenuItem)
      return ‘contextmenuitem’;
      if (obj instanceof Date)
      return ‘date’;
      if (obj instanceof Error)
      return ‘error’;
      if (obj instanceof LoadVars)
      return ‘loadvars’;
      if (obj instanceof LocalConnection)
      return ‘localconnection’;
      if (obj instanceof MovieClipLoader)
      return ‘moviecliploader’;
      if (obj instanceof NetConnection)
      return ‘netconnection’;
      if (obj instanceof NetStream)
      return ‘netstream’;
      if (obj instanceof PrintJob)
      return ‘printjob’;
      if (obj instanceof Sound)
      return ‘sound’;
      if (obj instanceof TextField)
      return ‘textfield’;
      if (obj instanceof TextFormat)
      return ‘textformat’;
      if (obj instanceof TextSnapshot)
      return ‘textsnapshot’;
      if (obj instanceof Video)
      return ‘video’;
      if (obj instanceof XML)
      return ‘xml’;
      if (obj instanceof XMLNode)
      return ‘xmlnode’;
      if (obj instanceof XMLSocket)
      return ‘xmlsocket’;
      if (obj instanceof XML)
      return ‘xml’;

      return ‘object’;
      }

    Comments are closed.