使用数组函数处理点击事件

using an array function for click events

我创建了包含动画的符号 (mc1 - mc25)。如果我单击符号(单击 mc1 -> 播放 mc1,单击 mc2 -> 播放 mc2 等),我想播放这些动画。

我创建了一个数组来一次解决我所有的符号。它工作正常。

var A:Array = [mc1, mc2, mc3, mc4,...mc25] // create array
car aClip:MovieClip;

for each (aClip in A) // stop all symbols
{aClip.stop();}

如何使用数组函数为我的所有交易品种获得以下结果?

mc1.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_4);
function fl_MouseClickHandler_4(event:MouseEvent):void
{
mc1.play();
}

我尝试过类似的方法,但无法正常工作:

aClip.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void {
aClip.play();
}

谢谢!

最简单的方法是通过算法计算出点击了哪一个。下面的脚本很短,不包含各种检查,它假定 A 的所有元素实际上都是 MovieClips.

// Assume, you filled the Array with these clips already.
var A:Array;

// Iterate all the clips.
for each (var aClip:MovieClip in A)
{
    // Subscribe to each of the clips for the CLICK event.
    aClip.addEventListener(MouseEvent.CLICK, clickPlay);
}

// Click event handler function. The point is, you can subscribe it
// to the multiple objects and use the function argument to figure out
// which one of them all is actually the source of the event.
// Furthermore, you can subscribe it to handle multiple events too,
// the argument also carries the id of the event. Sometimes it is
// really simpler to compose a single event-processing gate
// rather then process each one event in a separate handler.
function clickPlay(e:MouseEvent):void
{
    // Use e.currentTarget because the initial e.target
    // could be not the MovieClip itself, but some interactive
    // element deep inside the MovieClip's hierarchy.

    // Read about bubbling events (MouseEvent is bubbling)
    // to get the whole picture of it.

    // Typecasting, because e.target and e.currentTarget
    // are of base Object type.
    var aClip:MovieClip = e.currentTarget as MovieClip;

    // Well, the only thing that is left.
    aClip.play();
}