Assigning functions to movieclips in array

I’m creating a menu from mcs loaded from the library, I want to assign an onRelease function to each of the menu items which i’m storing in a temporary array when created… basically this: -


//loop and attach menu items for the number of items in layLength
for(var i=0, j=0; i < layLength; i++){
	var temp = container.attachMovie("menuItemMC", "menuItemMCxxx" + i, i);
	temp.img = as2obj.boat.img*.attributes.file; //some xml data
	tempArray.push(temp);
	y++;
}
 
//Assign onRelease events to each menu item loaded
for(g in tempArray){
	tempArray[g].onRelease = function() {
	trace("I am associated with " + tempArray[g].img);
}
}

the problem is the functions assigned to the created movieclips will just default to the current value of ‘g’ I.e. when trace is triggered it will trace the tempArray value of whatever g is currently, I want it to remember the value at the time the function is assigned to the movie.

Sorry for the poor explanation, my english is bad… I’m british :S

Thanks

Dan

Use [font=courier new]this[/font] to reference the object that the called function/method belongs to:

for (g in tempArray) {
	tempArray[g].onRelease = function() {
		trace("I am associated with "+this.img);
	};
}

That would do. :wink:

that simple!

what trees? can’t even see the forest!

Nice one, appreciated

No problem, Danny. :slight_smile:

And I’ve just realized that this is your first post, so… welcome to kirupaForum! :stuck_out_tongue: