を使用すると、同じイベントリスナーを追加することができ、クリップのそれぞれについて
- いくつかのループはどこかに適用する必要がありますが、間接的にループを抜け出すことができます.VMループをあなたに任せることができます。 Array.forEach
をご覧ください。シンプルなアプリケーションがあるかもしれない
:私はあなたが単純に配列のどこかにアクセスをキャッシュすることをお勧めしなければならないの
myArr.forEach(function(item:*, index:int, array:Array):void
{
item.addEventListener(MouseEvent.CLICK, function(event:Event):void
{
trace("my index is", index);
});
});
:
アイテムのインデックスを取得するには、より複雑なものを作るかもしれませんリスナー関数にArray.indexOfと一緒にevent.currentTargetを使用しますが、許容範囲外であればforEachを次のように使用できます。
あなたの特定のユースケースについての詳細を知らなくても
// probably not best to have this public
protected var myArr:Array = [/* whatever goes in here */]
public function register():void
{
myArr.forEach(addHandler);
}
protected function addHandler(item:IEventListener, index:int, arr:Array):void
{
item.addEventListener(MouseEvent.CLICK, myClickHandler);
}
protected function myClickHandler(event:MouseEvent):void
{
trace(event.currentTarget, "is at", myArr.indexOf(event.currentTarget));
}
しかし、私は確認することはできません:10
myArr.forEach(function(item:*, index:int, array:Array):void
{
item.addEventListener(MouseEvent.CLICK, function(event:Event):void
{
trace("my index is", array.indexOf(item));
});
});
あなたはこれらのメソッドを呼び出す頻度に応じて、単に私たちは、このために速くないかもしれません。
これはしばらくお待ちください。あなたが正しい答えを見つけた場合、答えを正しいものとしてマークしてください。 –