kill.robot.kill
August 9th, 2004, 03:26 PM
I have 4 buttons on the stage. I want to assign each button a unique action by using a for loop to assign the actions to these buttons
This example does not work:
for (i=0; i<4; i++) {
this["btn"+i].onRelease = function() {
trace("this"+[i]);
};
}
each button traces "this4"
this example does work!
for (i=0; i<4; i++) {
myBtn = this["mybtn"+i];
myBtn.num = i;
myBtn.onRelease = function() {
trace(this.num);
};
}
each button traces properly.
what I want to know is why, I'm happy that I got this code to work at all, but I would really like to know why the bottom one works, and the top doesn't, so I at least learn from this.
My guess is that it has to do with the incrementing variable ("i") inside the for loop, but I just want to be clear on this.
thanks
This example does not work:
for (i=0; i<4; i++) {
this["btn"+i].onRelease = function() {
trace("this"+[i]);
};
}
each button traces "this4"
this example does work!
for (i=0; i<4; i++) {
myBtn = this["mybtn"+i];
myBtn.num = i;
myBtn.onRelease = function() {
trace(this.num);
};
}
each button traces properly.
what I want to know is why, I'm happy that I got this code to work at all, but I would really like to know why the bottom one works, and the top doesn't, so I at least learn from this.
My guess is that it has to do with the incrementing variable ("i") inside the for loop, but I just want to be clear on this.
thanks