如何以编程方式访问 DisplayObject 的所有子级?

发布于 2024-10-03 01:02:38 字数 198 浏览 2 评论 0原文

如何使用代码访问 DisplayObject 的所有子级? (我正在寻找类似于movieclip.children的东西)

我在两种情况下使用它:

1)循环并重新定位封闭MovieClip的所有子项。

或者

2) 循环并删除 MovieClip 的所有子项

另外,这是一个 Flash CS5 项目。

How do access all of the children of a DisplayObject using code? (I'm looking for something like movieclip.children)

I'm using this in two cases:

1) To loop through and reposition all of the children of an enclosing MovieClip.

Or

2) To loop through and delete all of the children of a MovieClip

Also, this is a Flash CS5 project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

裂开嘴轻声笑有多痛 2024-10-10 01:02:38

这个循环将触及 movieclip foo 内的每个子元素。我不确定你要对它们做什么,但你可以在循环内运行你需要的任何方法。

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

This loop will touch every child inside movieclip foo. I'm not sure what you're going to do to them, but you can run whatever methods you need inside the loop.

for (var i:uint=0; i<foo.numChildren;i++){
    foo.getChildAt(i).whateverMethodYouNeed();
}
知你几分 2024-10-10 01:02:38

您的对象只是一个显示对象吗?如果它是 UIComponent,您可以使用 getChildAt() 和 getChildByName() 以及 numChildren 属性来循环它们。这是 Flex 项目的一部分还是仅 ActionScript 项目的一部分?

DisplayObject 本身没有描述其子对象的机制。了解子级的最低级别类型是 DisplayObjectContainer。您可能必须将该对象至少转换为 DisplayObjectContainer 才能执行您想要的操作。如果您有可以使用的 Flex 框架,我会选择 UIComponent。

DisplayObject

DisplayObjectContainer

UIComponent

Is your object just a display object? If it is an UIComponent, you could use getChildAt() and getChildByName() along with the numChildren property to loop over them. Is this part of a flex project or an actionscript only project?

A DisplayObject itself does not have the mechanisms to describe its children. The lowest level type that knows about children is the DisplayObjectContainer. You may have to convert the object into at least a DisplayObjectContainer to be able to do what you want. I would go with an UIComponent though if you have the flex framework to work with.

DisplayObject

DisplayObjectContainer

UIComponent

誰認得朕 2024-10-10 01:02:38

如果您需要访问所有孩子,包括孩子的孩子,您可以尝试
这:

    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;
    }

If you need to access all the children, inclusive of a child's children, you can try
this:

    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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文