2011-08-07 10 views
2

ユーザーがクリックできるボタンを表すムービークリップの配列があります。そのため、クリックを処理できるようにaddEventListener関数を使用する必要があります。actionscript内のすべての配列要素に対して1つのaddEventListenerを作成します。

私はループを使用して要素ごとにaddEventListenerを作成できますが、配列には26個の要素がありますが、addEventListenerを1つだけ使用して別のソリューションを試して、要素ではなく配列に適用します。

クリックされたボタンを認識する方法を知りたいのですが、配列のインデックスが何であるかを知っています。

ありがとうございました。

+0

これはしばらくお待ちください。あなたが正しい答えを見つけた場合、答えを正しいものとしてマークしてください。 –

答えて

0

ムービークリップに追加します。イベントリスナーを配列に追加しても意味がありません。あなたは基本的には "ねえの配列、あなたのことが何か変わったときに教えてください"と言っています.ArrayはEventDispatcherのサブクラスではありません。しかし、どのような場合でも配列について知りたくない場合は、ムービークリップについて知りたいので、論理的に行うことはループを作成してムービークリップに追加することです。

0

イベントリスナーを配列に割り当てることはできません。

あなたがやっていると思うことは、配列内の各クリップに異なるイベントリスナー機能を適用することです。

clips[i].addEventListener(MouseEvent.CLICK, handleClick);

と​​機能のようなものになります:あなたが直接ループから抜け出すことができない

function handleClick(e:MouseEvent):void { 
    trace(clips.indexOf(e.target)) // outputs index the movieclip that was clicked on 
} 
0

を使用すると、同じイベントリスナーを追加することができ、クリップのそれぞれについて

- いくつかのループはどこかに適用する必要がありますが、間接的にループを抜け出すことができます.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)); 
    }); 
}); 

あなたはこれらのメソッドを呼び出す頻度に応じて、単に私たちは、このために速くないかもしれません。

0

ボタン用のクラスを作成し、そのクラスにイベントリスナーを追加します。この方法では、ムービークリップをループする必要はありません。メソッドがクラスの一部になるため、

0

IEventDispatcherの独自のカスタムベクトルクラスを作成できます。カスタムメソッドでは、すべてのイベントリスナーを追加しますその要素。これを行う最善の方法は、Vector.<IEventDispatcher>オブジェクトのラッパーとして機能するプロキシクラスを作成することです。私はこれを実証するための例を作成しました:

package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 
    import flash.events.IEventDispatcher; 

    public class Main extends Sprite 
    { 
     private var _eventDispatcherVector:EventDispatcherVector; 

     public function Main():void 
     { 
      if (stage) init(); 
      else addEventListener(Event.ADDED_TO_STAGE, init); 

     }// end function 

     private function init(e:Event = null):void 
     { 
      removeEventListener(Event.ADDED_TO_STAGE, init); 

      var redCustomButton:CustomButton = new CustomButton("RED", 0xFF0000); 
      addChild(redCustomButton); 
      var blueCustomButton:CustomButton = new CustomButton("BLUE", 0x00FF00); 
      blueCustomButton.x = 100; 
      addChild(blueCustomButton); 
      var greenCustomButton:CustomButton = new CustomButton("GREEN", 0x0000FF); 
      greenCustomButton.x = 200; 
      addChild(greenCustomButton); 

      _eventDispatcherVector = new EventDispatcherVector(Vector.<IEventDispatcher>([redCustomButton, 
                          blueCustomButton, 
                          greenCustomButton])); 

      _eventDispatcherVector.addEventListener(MouseEvent.CLICK, onCustomButtonClick); 

     }// end function 

     private function onCustomButtonClick(e:Event):void 
     { 
      var customButton:CustomButton = e.target as CustomButton; 

      trace("You clicked: " + customButton.name + "\n" + 
        "Its index is: " + _eventDispatcherVector.indexOf(customButton)); 

     }// end function 

    }// end class 

}// end package 

import flash.utils.Proxy; 
import flash.utils.flash_proxy; 
import flash.events.IEventDispatcher; 

use namespace flash_proxy; 

dynamic internal class EventDispatcherVector extends Proxy 
{ 
    private var _eventDispatcherVector:Vector.<IEventDispatcher>; 

    public function EventDispatcherVector(eventDispatcherVector:Vector.<IEventDispatcher>) 
    { 
     _eventDispatcherVector = eventDispatcherVector; 

    }// end function 

    override flash_proxy function getProperty(name:*):* 
    { 
     return _eventDispatcherVector[name]; 
    } 

    override flash_proxy function setProperty(name:*, value:*):void 
    { 
     _eventDispatcherVector[name] = value; 

    }// end function 

    public function indexOf(searchElement:*, fromIndex:*= 0):int 
    { 
     return _eventDispatcherVector.indexOf(searchElement, fromIndex); 

    }// end function 

    public function addEventListener(type:String, 
            listener:Function, 
            useCapture:Boolean = false, 
            priority:int = 0, 
            useWeakReference:Boolean = false):void 
    { 
     for each(var eventDispatcher:IEventDispatcher in _eventDispatcherVector) 
     { 
      eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference); 

     }// end for each 

    }// end function 

}// end class 

import flash.display.Sprite; 
import flash.text.TextField; 
import flash.text.TextFieldAutoSize; 

internal class CustomButton extends Sprite 
{ 
    public function CustomButton(name:String, color:uint) 
    { 
     graphics.beginFill(color); 
     graphics.drawRect(0, 0, 100, 100); 
     graphics.endFill(); 

     var textField:TextField = new TextField(); 
     textField.autoSize = TextFieldAutoSize.LEFT; 
     textField.text = name; 
     textField.mouseEnabled = false; 
     textField.x = (100/2) - (textField.width/2); 
     textField.y = (100/2) - (textField.height/2); 
     addChild(textField); 

     this.name = name; 

    }// end function 

}// end class 

CustomButtonオブジェクトをクリックしてからの出力は以下の通りです:

あなたがクリックした:RED
そのインデックスは次のとおりです。あなたがクリックした0
:BLUE
そのインデックスは次のとおりです。1
あなたがクリックした:GREEN
そのインデックスは次のとおりです。2

詳細については、質問Actionscript 3.0 Best Option for Subclassing Vector Class (Flash Player 10)のStigglersの回答をご覧ください。

6

これはおそらく、イベントのバブリングについて学ぶのに良い時期です。あなただけの、試してみて、

private function buttonContainerClickHandler(e:MouseEvent):void 
{ 
    var targetButton:Sprite = e.target as Sprite; 
    //do something with targetButton. 
} 

がクリックされたものを、ボタンを見つけるには、クリックされたものをうまくすべてのボタン

その後
buttonContainer.addEventListener(MouseEvent.CLICK, buttonContainerClickHandler); 

の共通の親へのリスナーを追加することができ、あなたはindexOfメソッドを使用することができますあなたの配列の配列を取得し、それにtargetButtonを渡します。

あなたがしなければならないことは、各ボタンにmouseChildrenがfalseに設定されていることを確認することです。e.targetはボタンの子アセットを返します。

+2

+1 - 私はこれを書いている途中で、まったく同じ提案です。このページの他のすべての答えは、尋ねられる1つのことを実行することができません:単一のイベントリスナーを使用します。理由の声でありがとう! – merv

関連する問題