Recursive display list tree traversal : stage « Development « Flash / Flex / ActionScript






Recursive display list tree traversal

 
package {
  import flash.display.*;
  import flash.events.*;

  public class Main extends Sprite {
    public function Main (  ) {
      var rects:Array = new Array(  );
      for (var i:int = 0; i < 20; i++) {
        rects[i] = new Shape(  );
        rects[i].graphics.lineStyle(1);
        rects[i].graphics.beginFill(Math.floor(Math.random(  )*0xFFFFFF), 1);
        rects[i].graphics.drawRect(0, 0, 100, 50);
        rects[i].x = Math.floor(Math.random(  )*500);
        rects[i].y = Math.floor(Math.random(  )*400);
        addChild(rects[i]);
      }

      stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener);
    }

    private function mouseDownListener (e:Event):void {
      for (var i:int=0; i < numChildren; i++) {
        getChildAt(i).rotation = Math.floor(Math.random(  )*360);
      }
    }
    
    public function processChildren (container:DisplayObjectContainer):void {
      for (var i:int = 0; i < container.numChildren; i++) {
        var thisChild:DisplayObject = container.getChildAt(i);
        trace(thisChild.toString(  ));
    
        if (thisChild is DisplayObjectContainer) {
          processChildren(DisplayObjectContainer(thisChild));
        }
      }
    }
  }
}

        








Related examples in the same category

1.Setting the Frame Rate
2.Traversing Objects in a Display Hierarchy
3.Distinguishing Events Targeted at an Object from Events Targeted at That Object's Descendants
4.responds to mouse clicks that occur over any object on the display list, but not over a vacant area of the Stage instance
5.A stretchy layout
6.Simple Scribble
7.Working with the Stage
8.Local Global Sample