# Array and For loop problem: borrowing

**URL:** <https://forum.kirupa.com/t/array-and-for-loop-problem-borrowing/65785>\
**Category:** flash\
**Created:** [September 29, 2004, 4:39pm UTC](https://forum.kirupa.com/t/array-and-for-loop-problem-borrowing/65785 "2004-09-29T16:39:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![marc0047](https://avatars.discourse-cdn.com/v4/letter/m/ce73a5/32.png) [@marc0047](https://forum.kirupa.com/u/marc0047)\
**Post date:** [September 29, 2004, 4:39pm UTC](https://forum.kirupa.com/t/array-and-for-loop-problem-borrowing/65785/1 "2004-09-29T16:39:32Z")

</div>

I am trying to make use of two ‘arrays’ in one ‘for’ loop. The two arrays are called ‘overlay’ and ‘button’. The contents of both arrays all refer to movieclip instances on the stage. What is happening is that the corresponding ‘overlay\*’ button will perform the alpha change according to a rollover.

However, the state of the corresponding ‘button\*’ will not respond and change its alpha state. I did a trace for ‘button\*’ and ‘undefined’ is returned. I thought it might be a level referencing problem, so I made all the instances in the button array have ‘\_root.’. but that didn’t solve the problem either.

The following is the for loop. Anyone have any suggestions?

for ( var i = 0; i \< overlay.length; i++ ) {  
overlay\*.onRollOver = function () {  
this.\_alpha = 50;  
button\*.\_alpha = 50;  
trace (button\*); // returns ‘undefined’  
}  
}

---

<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:** [September 29, 2004, 6:12pm UTC](https://forum.kirupa.com/t/array-and-for-loop-problem-borrowing/65785/2 "2004-09-29T18:12:21Z")

</div>

Use a prototype…  
In the prototype define the onRollOver function  
See the fla is there a set of btns with a rollOver aplied to all

---

<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:** [September 29, 2004, 6:12pm UTC](https://forum.kirupa.com/t/array-and-for-loop-problem-borrowing/65785/3 "2004-09-29T18:12:44Z")

</div>

When the actual onRollOver happens, the for loop has already finished by far then, and the i variable is lost. So store it in a property during the loop so it can be retrieved later:

```auto

for (var i=0;i<overlay.length;i++) {
overlay*.id = i;
overlay*.onRollOver = function () {
this._alpha = 50;
button[this.id]._alpha = 50;
trace (button[this.id]);
}
}

```

---

<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:** [September 29, 2004, 9:37pm UTC](https://forum.kirupa.com/t/array-and-for-loop-problem-borrowing/65785/4 "2004-09-29T21:37:08Z")

</div>

I would also do it as Voetsjoeba has explained but i don`t think you should use button as the name of an array and also if everything is on the same timeline you will have to reference it slightly differently  
maybe  
for (var i = 0; i\<overlay.length; i++) {  
overlay\*.id = i;  
overlay\*.onRollOver = function() {  
this.\_alpha = 50;  
this.\_parent.buttonnewname[this.id].\_alpha = 50;  
trace(this.\_parent.buttonnewname[this.id]);  
};  
}

---

<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:** [September 30, 2004, 6:18pm UTC](https://forum.kirupa.com/t/array-and-for-loop-problem-borrowing/65785/5 "2004-09-30T18:18:10Z")

</div>

Thanks all. I’m beginning to understand.

One implementation I don’t understand is ‘[this.id]’. Can someone provide an example of what goes into the brackets?

---

<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:** [September 30, 2004, 6:22pm UTC](https://forum.kirupa.com/t/array-and-for-loop-problem-borrowing/65785/6 "2004-09-30T18:22:03Z")

</div>

Thanks all. I understood it conceptually, but didn’t understand the implementation until I tried it.

Thank you!
