Recursion For The Timeline In Flash

23 July 2009

This morning in the train I worked on a little script for recursion. I need this to update a timeline animation on the fly.

With this script you get an Array with every DisplayObject on the currentframe in a DisplayObjectContainer (MovieClip, Sprite etc).

package test {
  import flash.display.DisplayObject;
  import flash.display.DisplayObjectContainer;
 
  /**
   * @author Jankees.van.Woezik
   */
  public class Recursion {
 
    private var childs : Array;
 
    public function getAllChilds(inMC : DisplayObjectContainer) : Array {
      childs = new Array();
      findChilds(inMC);
      return childs;
    }
 
    private function findChilds(inDisplayObjectContainer : DisplayObjectContainer,inCurrentDepth : uint = 0) : void {
 
      var i : uint = inDisplayObjectContainer.numChildren;
      var child : DisplayObject;
 
      while( i -- ) {
        child = inDisplayObjectContainer.getChildAt(i);
 
        if(child is DisplayObjectContainer) {
          childs.push(child);
          findChilds(DisplayObjectContainer(inDisplayObjectContainer.getChildAt(i)), inCurrentDepth + 1);
        }else if(child is DisplayObject) {
          childs.push(child);
        }
      }
    }
  }
}
Jankees van Woezik profile picture

Hello, I'm Jankees van Woezik

Like this post? Follow me at @jankeesvw on Twitter