all groups > flash actionscript > july 2005 >
You're in the

flash actionscript

group:

MovieClip in Array


MovieClip in Array malachite00
7/20/2005 10:37:58 PM
flash actionscript:
I have an array which I have populated with 4 movie clips. All the clips are
instances of the same mc. They are all clickable, so I attach an onPress() to
each in the array. The correct function runs when a clip is pressed, however I
am unable to tell which one in the array was actually clicked. Here is the for
loop:

for( var i =0; i < small_clips.length; i++)
{
curr_mc = small_clips;
curr_mc.onPress = function()
{
clicked = true;
this.setSelection(i);
}
}

The problem is I can access the clicked one via "this" but I don't know which
it was with respect to the rest of the array. The "i" that is passed is always
the last one in the array, regardless of which clip is pressed. How can I
determine which one is being pressed so I can find it later in the array?

Thanks,

--amy
Re: MovieClip in Array mandingo
7/20/2005 10:48:22 PM
that is because by the time you click on the instance, the variable incrementer
has reached the end... to do what you want you need to store the index against
the clip instance itself at the time of creation...

try:

for (var k = 0; k < small_clips.length; k++) {
curr_mc = small_clips[k];
curr_mc.arrayIndex = k;
curr_mc.onPress = function() {
clicked = true;
trace(this.arrayIndex);
};
}

note too when using these forums, an i inside [] (square brackets) renders
italics and most people will lose most of your code... use something like k or
n... or z.

cheers,

Re: MovieClip in Array malachite00
7/21/2005 12:16:29 AM
Thanks mandingo, that works perfectly! And thanks for the tip on using i - I didn't realize that it switched to italic.

Re: MovieClip in Array mandingo
7/21/2005 12:41:39 AM
AddThis Social Bookmark Button