如何递归访问孩子的孩子? [英] How to access children of children recursively?

查看:29
本文介绍了如何递归访问孩子的孩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Canvas,里面有很多组件,而那些里面有很多组件.

I have a Canvas which has many components inside it and those again, have many components inside them.

getChildren() 只返回顶级的孩子.检索所有子项(子项的子项的子项等)的最佳方法是什么.

getChildren() returns only the top level children. What is the best way to retrieve all the children (children of children of children and so on).

好吧,我有点知道如何通过迭代子项来做到这一点,但是代码真的很混乱.我更喜欢使用一个很好的递归函数.以前有人写过这个吗?或者是否有 Util 类可以执行此操作?

Well, I sorta know how to do this by iterating through the children, but the code is really messy. I'd prefer to use a nice recursive function. Has anyone written this before? Or is there a Util class to do this?

推荐答案

private function recuse(display : DisplayObjectContainer) : void {
  if(display) {
    for (var i : int = 0;i < _display.numChildren;i++) {
        var child : DisplayObject = display.getChildAt(i);
        trace(child.name);
        if(child is DisplayObjectContainer) {
            recuse(DisplayObjectContainer(child));
        }
    }
}

}

这篇关于如何递归访问孩子的孩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆