A perennial actionscript question
By: Jeremy Knight
This is a problem I always have and it always takes me a while to remember the solution. Whenever you are creating a bunch of buttons at once, you typically create a for loop with an onRelease function within:
for(i=1 ; i < number_of_buttons ; i++) {
this[\"button\"+i].onRelease = function() {
trace(\"button has been pressed\");
}
}
The tricky thing comes when you try to use the value of i, from within the onRelease function. You can\'t do it, because at the time you click the button I will equal who knows what. What you can do though, is create another variable within the button and assign \"i\" to that. Thusly:
for(i=1 ; i < number_of_buttons ; i++) {
this[\"button\"+i].button_number = i;
this[\"button\"+i].onRelease = function() {
trace(\"button #\"+button_number+\" has been pressed\");
}
}
Side note: Before I discovered this seemingly simple maneuver I would take the string value of \\\"this\\\" then remove everything but the number on the end. This works, but is overly complicated and also gets screwed up if you ever decide to move the button to another timeline.
You see all those \\ marks before the double quotes (\") - those shouldn\'t be there. Whoever developed this site (me) messed up the escaping code. I just don\'t have to time to fix it right now.