Domanda

I'm using following code to register a function for click event of all SimpleButtons inside my swf file. The SimpleButtons that their name begins with 'b' must be register. But it not works for all buttons. Some of buttons in another MovieClip or another frames will not visible! 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! :(

È stato utile?

Soluzione

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). 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).

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top