Set multiple Button events with a for loop

Question:

I have the following piece of script. Works like charm, and why should it not.

_root.card1.but.onRelease = function() {
_root.card1.gotoAndStop(2);
if (_root.firstcard==""){
_root.firstcard=1;
} else if (_root.secondcard==""){
_root.secondcard=1;
}
}

The thing is, i have to ad this script to 55 more buttons. (_root.card2.but, _root.card3.but, etc) Instead of making 56 of the scripts, I tried to use a for loop. This is the loop I used.

for (i=1; i<=56; i++){
_root[“card”+i].but.onRelease = function() {
_root[“card”+i].gotoAndStop(2);
if (_root.firstcard==""){
_root.firstcard=i;
} else if (_root.secondcard==""){
_root.secondcard=i;
}
}
}

But it does not seem to work. Can anybody lend me a hand?

Thanks!

Please use the AS vbCode tags :sigh:

Try something like this…
[AS]for (i=1; i<=56; i++){
_root[“card”+i].but.i = i;
_root[“card”+i].but.onRelease = function() {
this.gotoAndStop(2);
if (_root.firstcard == “”){
_root.firstcard = this.i;
} else if (_root.secondcard == “”){
_root.secondcard = this.i;
}
}
}[/AS]

The variable “i” cannot be read in the onRelease, so I had to dynamically create a variable “i” attached to the _root[“card”+i].but clip. I could then call the variable as “this.i”

Should work… in theory. :stuck_out_tongue: (everyone knows how my theorys always end up)