RSS
 

Archive for the ‘Flash Actionscript’ Category

Actionscript 3 using string or integer to access variables

18 Aug

Here’s a situation I often run into: Having a set of variables that are almost alike, but differ in number. For example:

1
2
var textbox1:TextField;
var textbox2:TextField;

Now say I have a function that needs to change these based on a number in another variable. I could do a switch to access them, but that would be repeating myself. Instead I can just access them through my string or integer variable like this:

1
2
var num:int = 1;
trace(this["textbox" + num])

You could, for instance use it to loop over your variables:

1
2
3
4
5
function update_textfield() {
   for each(var i:int = 5; i < something; i++) {
      trace(this["textbox" + i])
   }
}

And presto! Easily accessable variables :)