How can I access all DisplayObject children programmatically?

How to access all DisplayObject children using code? (I'm looking for something like movieclip.children)

I use this in two cases:

1) Skip and move all child objects of the MovieClip environment.

or

2) To skip and delete all MovieClip child elements

It is also a Flash CS5 project.

+3
source share
3 answers

This cycle will concern each child inside movieclip foo. I'm not sure what you are going to do with them, but you can run any methods you need inside the loop.

for (var i:uint=0; i<foo.numChildren;i++){
    foo.getChildAt(i).whateverMethodYouNeed();
}
+6
source

? UIComponent, getChildAt() getChildByName() numChildren, . flex ActionScript?

DisplayObject . , , - DisplayObjectContainer. , , , DisplayObjectContainer, , . UIComponent, , flex framework .

DisplayObject

DisplayObjectContainer

UIComponent

0

, -, :

    function doWhatever( mc:DisplayOjectContainer ):void
    {
          if( mc.numChildren > 0 )
             for( var i:int ; i < mc.numChildren ; ++i )
             {
                 //if you need to reposition
                //set the points properties here
                var point:Point = new Point( _x , _y );
                setPosition ( mc.getChildAt(i ) , point );

                //if you need to remove all children
                //do it recursively
                //remove( mc , mc.getChildAt( i );
             }
    }

    function setPosition(mc:DisplayObject , point:Point ):void
    {
        mc.x = point.x ;
        mc.y = point.y;
    }

    function remove(container:DisplayObjectContainer , child:DisplayObject ):void
    {
         //this will remove all children before being removed
         if( child is DisplayObjectContainer )
         {
             var doc:DisplayObjectContainer = child as DisplayObjectContainer;
             doWhatever( doc );
         }

         container.removeChild( child );
         child = null;
    }
0

Source: https://habr.com/ru/post/1775080/


All Articles