How get all buttons in AIR/Flash
I'm using following code 开发者_StackOverflowto register a function for click event of all SimpleButton
s inside my swf
file.
The SimpleButton
s 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! :(
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.
精彩评论