Reputation: 722
Say, I have an array of movie clips (dummies). Each dummy has an event listener and reacts to clicks. And when a dummy is clicked, I want to know the index of the clicked dummy.
I've only come up with this solution: run through the whole array of dummies and find out which dummy is the target, then exit the 'for' cycle and assign some global variable the value of counter variable, but this way the application I am building will demontsrate poor performance.
Thanks in advance.
Upvotes: 0
Views: 850
Reputation: 13225
You want to use Array.indexOf() or Vector.indexOf(). Assuming dummies
is your global Array
or Vector.<Dummy>
variable:
function onClick(evt:MouseEvent):void {
var clickedDummy:Dummy = evt.target as Dummy;
var dummyIndex:int = dummies.indexOf(clickedDummy);
trace("You clicked the dummy at index " + dummyIndex);
}
Some quality time with the ActionScript 3.0 Reference for the Adobe Flash Platform will reward you. Start with the links here.
Upvotes: 1
Reputation: 2743
If you are adding the dummies programaticly, you can create a class that extends MovieClip called dummy and pass in the index value to the constructor when initialized.
Edit - This will give you are starting point
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Dummy extends MovieClip {
var id = -1;
function Dummy(id:int) {
this.id = id;
this.addEventListener(MouseEvent.CLICK, onClick);
}
function onClick(evt:MouseEvent):void {
// Handle click
}
}
Upvotes: 1