Frage

i've created an array. Each element is a button object. Is there a possibility to hook mouseclick on every array at the same time? I mean something like this.

var Objects:Array = new Array
Objects[0] = new button(parameters)
Objects[1] = new button(parameters)
Objects[2] = new button(parameters)

Objects[n].addEventListener(MouseEvent.CLICK, Clicked(n));

function Clicked(n,...)
{
      THECODE PROCEEEEDS for Objects[n]
}

I know that's not the clearest and most correct writing, but I'm asking if this is possible in similiar way? And how to do it? I know I can hook every mouseclick and then check if the clicked under the mouse is one of the array elements with for loop, but I'm asking about this way.

War es hilfreich?

Lösung

Yes. You are unable to directly pass an index into a listener, but you can retrieve that via calling indexOf() inside it.

for each (b in Objects) b.addEventListener(MouseEvent.CLICK, clicked);
// note, you just put function name here!
public function clicked(e:MouseEent):void {
    var i:int=Object.indexOf(e.target);
    if (i==-1) {
        // panic behavior
        return; 
    }
    // now you can parse that index into something valuable
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top