Array and For loop problem: borrowing

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’
}
}

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

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:


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]);
}
}

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]);
};
}

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?

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

Thank you!