简体   繁体   English

如何获取AIR / Flash中的所有按钮

[英]How get all buttons in AIR/Flash

I'm using following code to register a function for click event of all SimpleButton s inside my swf file. 我正在使用以下代码为swf文件中的所有SimpleButton的click事件注册一个函数。 The SimpleButton s that their name begins with 'b' must be register. 名称以'b'开头的SimpleButton必须进行注册。 But it not works for all buttons. 但这不适用于所有按钮。 Some of buttons in another MovieClip or another frames will not visible! 其他MovieClip或其他帧中的某些按钮将不可见! I Call this method inside first frame of first layer. 我在第一层的第一帧内调用此方法。

findChilds(this);

function findChilds(obj:*):void
{
    if (obj == null)
    {
        return;
    }
    //trace(obj.name);

    if (obj.name.substr(0,1) == "b")
    {
        obj.addEventListener(MouseEvent.CLICK, onMediaClicked);
        trace(obj.name, " registered for click.");
    }
    try 
    {
        // some type of objects hasn't numChildren property, so i 
        // used try/catch statement, i know this way has bad performance. I fix it later
        for (var i:int = 0; i < obj.numChildren; i++)
        {
            findChilds(obj.getChildAt(i));
        }
    }
    catch (e:Error)
    {
    }
}

Please Help! 请帮忙! :( :(

You need to call findChild(this) on every frame because if you call it on the first frame only the objects in subsequent frames are not loaded yet (and thus innaccessible). 您需要在每个帧上调用findChild(this) ,因为如果在第一帧上调用它,则仅后续帧中的对象尚未加载(因此无法访问)。 You might want to use hasEventListener to make sure you don't add the same event listener twice (or use some array to keep track of which frames you called findChild(this) on). 您可能要使用hasEventListener来确保不会两次添加相同的事件侦听器(或使用某个数组来跟踪您调用findChild(this)帧)。

In any case, your method is not a very good one and would be a nightmare to maintain. 在任何情况下,您的方法都不是一种很好的方法,将是维护噩梦。 You should try to add the event listeners "manually". 您应该尝试“手动”添加事件监听器。 It probably won't take that much more work and it will be easier to maintain in the long run. 从长远来看,它可能不会花费太多的工作,并且将更易于维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM