1 Reply Latest reply on Jul 27, 2006 5:48 AM by a.neko®

    Accessing nested UIComponents

    a.neko®
      The aim of the script is to be able to access component's chldren, and their children, and their children, etc... by their index.

      I've tried the following:

      // script

      public function deployNesting():void {

      var container:Canvas= new Canvas();
      var chldA:Canvas = new Canvas();
      var chldB:Canvas = new Canvas();
      var chldC:Canvas = new Canvas();

      this.addChild(this.container);
      this.container.addChild(chldA);
      this.container.addChild(chldB);
      this.container.addChild(chldC);
      return;
      }

      public function accessNesting():void {
      trace(this.childAt(0).childAt(0)) // error 1119*
      trace(this.childAt(0).numChildren) // error 1119*
      return;
      }

      *1119 : Access of possibly undefined property numChildren through a reference with static type flash.display:DisplayObject.


      I'm aware that such nesting is not usually a best practice, but in some cases it is required, and I would appreciate an advice on this issue.

      a.neko
        • 1. Accessing nested UIComponents
          a.neko® Level 1
          Well... It's time to assume the therapeutical values of this forum in my particular case : ) One more time, just after recurring to all posiible ways to solve it, and afterwards posting a topic here, I change my approach and find the solution...

          In this case it is required to cast the return of all the getChildAt() in the nesting structure, except the last one.

          You can access a nested child to add a new child to it's display list as follows:

          Canvas(this.getChildAt(0)).addChild(chldC);

          Accessing a deeply nested child's name property looks as follows:

          Canvas((Canvas(this.getChildAt(0))).getChildAt(0)).getChildAt(0).name;

          etc...

          Somewhat complicated, but solves the problem of accessing nested children when, for instance, the variables used to create them are gone, or to freely iterate through nested display lists...

          (Verified with Flex 2.0 Builder mx.* components, but I suppose it works also for flash.display.* objects.)

          a.neko