displayobject_在AS3显示列表的任何深度查找DisplayObject

displayobject

Sometimes you know that one object has a specific child in it, but you can't find the child.

有时您知道一个对象中有一个特定的子对象,但找不到该子对象。

This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar using a third party Flash class, and I knew the name of the toolbars and the buttons, but I couldn't access the button.  No syntax I tried would let me get to it.  I tried myToolbar.myButton, myToolbar["myButton"] and myToolbar.getChildByName( "myButton" ), but none of them gave me the button.  getChildByName returned null, which I knew meant that myButton wasn't a child of myToolbar.  Iterating through all the children of myToolbar, all I found were a lot of objects named with "instance" followed by a number.

当我尝试编写一些actionScript来使工具栏使用其嵌入式按钮时,这发生在我身上。 我的伙伴使用第三方Flash类创建了工具栏,我知道工具栏和按钮的名称,但是我无法访问该按钮。 我尝试过的任何语法都不会帮助我。 我尝试了

We checked the work my partner had done.  He had properly named each button, but I wasn't finding it where I thought I would.

我们检查了我的伴侣所做的工作。 他已经正确命名了每个按钮,但我找不到我认为可以的位置。

To make a long day of research short, it eventually became obvious that the button wasn't an immediate child of the toolbar.  The "instance" names seem to be pretty common to Flash; they are generated frequently in objects designed in Flash.

为了缩短一整天的研究时间,最终变得很明显,该按钮不是工具栏的直接子项。 “实例”名称似乎在Flash中很常见; 它们经常在Flash中设计的对象中生成。

This question on EE got me pointed in the right direction.  I used the code from 有关EE的问题使我指出了正确的方向。 我使用的代码从 ugeb to dump the display lists of ugeb倾倒 myToolbar.  The third-party toolbar software apparently added the button three levels deep.  We started doing a horrible thing - hard-coding to the "instance" variables.  This produced some ugly code:
var i117:DisplayObjectContainer = myToolbar.getChildByName( "instance117" ) as DisplayObjectContainer;
var i189:DisplayObjectContainer = i117.getChildByName( "instance189" ) as DisplayObjectContainer;
var i288:DisplayOjbectContainer = i189.getChildByName( "instance288" ) as DisplayObjectContainer;
var myButton:DisplayObject = i288.getChildByName( "i288" ) as DisplayObject;

This worked on my machine, but is only acceptable if you never add any more children to the display list.  This is not robust, and therefore not good.  I realized that I could modify the code that traces each child in the display list to simply find the child you want.  The code below shows the function I used:

这在我的机器上有效,但是仅当您从未在显示列表中添加更多子代时才可接受。 这不是鲁棒的,因此不好。 我意识到我可以修改跟踪显示列表中每个孩子的代码,以简单地找到所需的孩子。 下面的代码显示了我使用的功能:

function findChild( dispobj:DisplayObjectContainer, childname:String ):DisplayObject
{
    if (dispobj == null)
    {
        dispobj = this;
    }
 	
    for (var j:int = 0; j < dispobj.numChildren; ++j)
    { 
        var obj:DisplayObject = dispobj.getChildAt( j ) as DisplayObject;
        if (obj.name == childname)
        {
            return obj;
        }
        if (obj is DisplayObjectContainer)
        {
            var doc:DisplayObjectContainer = obj as DisplayObjectContainer;
            if (doc.numChildren > 0)
            {
                var ret:DisplayObject = findChild( doc, childname );
                if (ret != null)
                {
                    return ret;
                }
            }
        } 
    }
    return null;
}

var myButton:DisplayObject = findChild( myToolbar, "myButton" );

var myButton:DisplayObject = findChild(myToolbar,“ myButton”);

This will run through every child of myToolbar, looking for one named "myButton".  If it doesn't find any, it will return null.

这将遍历myToolbar的每个子项,寻找一个名为“ myButton”的子项。 如果找不到,它将返回null。

If you want to search the entire displaylist, simply pass null as the first argument.

如果要搜索整个显示列表,只需将

By using this code, I made my code much more robust, and got the buttons hooked up easily.  Any time you can't find a DisplayObject where it should be, you can use this to find it for you.

通过使用此代码,我使我的代码更加健壮,并且使按钮易于连接。 任何时候找不到合适的DisplayObject时,都可以使用它为您找到它。

翻译自: https://www.experts-exchange.com/articles/2517/Finding-a-DisplayObject-at-any-depth-of-the-AS3-Display-List.html

displayobject

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值