Flex Child Management

Flex containers are different from normal Flash DisplayObjectContainer. They hide some of their children when you try to access by a children counting method- container.numChildren. By default when no children is attached to the containers, they show numChildren value as 0, but they do contain chidren, which are known as”chrome” elements or “non-content” children. Let’s evaluate the below code to verify it.( This code is set up by adding a new

  1. public function init():void{
  2.         this.addEventListener(MouseEvent.MOUSE_UP,checkOut);
  3. }
  4. public function checkOut(evt:MouseEvent):void{
  5.         trace(container.numChildren);//returns 0
  6.         var arrNodes:Array=container.getObjectsUnderPoint(new Point(mouseX, mouseY));
  7.         if(arrNodes.length){
  8.                 trace(arrNodes[0] ); //testApp.container.border
  9.                 trace(container.getChildIndex(arrNodes[0]));// -1
  10.         }
  11. }

As container has no child attached to it, numChildren property traces 0, where as getObjectUnderPoint(new Point(mouseX, mouseY)); returns a single element array. The reason comes from the implementation of the child management in flex framework- Flex maintains it’s children in two different partition (content children and non-content children). Index for all the children is contiguous but content children start after the non-content children. Container maintains two state variables, _firstChildIndex and _numChildren, which keep track of the partitioning. Components that extends Container may have more non-content elements depending upon the complexity.

This entry was posted in Flex, Flex 3.0, Flex Hacks. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>