如何在AS3中的舞台中检索所有MC?

时间:2011-08-04 08:58:45

标签: flash actionscript-3 flash-cs5

如何在舞台中检索所有MC?

我想为我的舞台中存在的所有MovieClip调用addEventListener,如果可以在选择的我的场景的帧编号中 像伪代码那样的东西

for(i=0; nbOfChild ; i++)
   if(stage.childAt(i) is MC and isInTheFrameWithLabel('foo') )
     stage.childAt(i).addEventListerner(MyStuff)

2 个答案:

答案 0 :(得分:9)

几乎就是你拥有的......

for(var i:int = 0; i<numChildren; i++)
{
    var e:DisplayObject = getChildAt(i);
    if(e is MovieClip)
    {
        // do stuff with e
        trace(e);
    }
}

你也可能是一个棘手的骗子,并做这样的事情:

/**
 * 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;
});

答案 1 :(得分:0)

您还应该将这些影片剪辑推送到数组,以便了解触发事件的影片剪辑,所以您可能会喜欢:

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);
    }
}