# Assigning functions to movieclips in array

**URL:** https://forum.kirupa.com/t/assigning-functions-to-movieclips-in-array/59245
**Category:** flash
**Created:** [July 29, 2004, 4:28pm UTC](https://forum.kirupa.com/t/assigning-functions-to-movieclips-in-array/59245 "2004-07-29T16:28:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![DannyT](https://avatars.discourse-cdn.com/v4/letter/d/779978/32.png) [@DannyT](https://forum.kirupa.com/u/DannyT)
#### Post date: [July 29, 2004, 4:28pm UTC](https://forum.kirupa.com/t/assigning-functions-to-movieclips-in-array/59245/1 "2004-07-29T16:28:06Z")

</div>

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: -

```auto

//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

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 29, 2004, 4:36pm UTC](https://forum.kirupa.com/t/assigning-functions-to-movieclips-in-array/59245/2 "2004-07-29T16:36:36Z")

</div>

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

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

```

That would do. 😉

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 29, 2004, 4:43pm UTC](https://forum.kirupa.com/t/assigning-functions-to-movieclips-in-array/59245/3 "2004-07-29T16:43:19Z")

</div>

that simple!

what trees? can’t even see the forest!

Nice one, appreciated

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 29, 2004, 4:45pm UTC](https://forum.kirupa.com/t/assigning-functions-to-movieclips-in-array/59245/4 "2004-07-29T16:45:45Z")

</div>

No problem, Danny. 🙂

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