How can I retrieve all MC in a Stage in AS3?
How can I retrieve all MC in a Stage?
I want to call addEventListener for all MovieClip present in my stage and if it's possible in a selected frame number of my scenario Something like that pseudo code
for(i=0; nbOfChild ; i++)
if(stage.childAt(i) is MC and isInTheFrameWithLabel('foo') )
stage.childAt(i).addEventListerner(MyStuf开发者_Python百科f)
Pretty much exactly what you have..
for(var i:int = 0; i<numChildren; i++)
{
var e:DisplayObject = getChildAt(i);
if(e is MovieClip)
{
// do stuff with e
trace(e);
}
}
You could also be a tricky trickster and do something like this:
/**
* Iterate through MovieClips within a container and parse them through handler
* @param container The container to iterate through and find MovieClips in
* @param handler A function that accepts MovieClip as its only parameter
*/
function each(container:DisplayObjectContainer, handler:Function):void
{
for(var i:int = 0; i<container.numChildren; i++)
{
var e:DisplayObject = container.getChildAt(i);
if(e is MovieClip) handler(e);
}
}
// Example
each(this, function(mc:MovieClip):void
{
trace(mc);
mc.x += 10;
});
you should also push those movie clips to an array in order to know the movie clip who has trigger the event, so you would have like:
var holdObjects:Array = new Array();
for(var i:int = 0; i<numChildren; i++)
{
var e:DisplayObject = getChildAt(i);
if(e is MovieClip)
{
// do stuff with e
trace(e);
e.addEventlistene(...);
holdObjects.push(e);
}
}
精彩评论