I'm trying to create a number of similar variables using a for loop, rather than having to explicitly write out each variable.
For instance, I need 9 variables called zone1, zone2, zone3 etc.
So rather than writing it out like so...
public var zone0:Zone = new Zone();
public var zone1:Zone = new Zone();
public var zone2:Zone = new Zone();
// etc. etc.
After a look around on the internet, I came up with this, however, it doesn't work...
for (var i:uint = 0; i<=8; i++)
public var this["zone" + i]:Zone = new Zone()
Any pointers greatly appreciated!
One way to approach it may be to try using an array and then assign each array element as a new Zone object.
public var zone:Array = new Array();
Indeed you can use
this["zone" + i] = new Zone();
After that you can access your variable either using
this["zone" + 2] or this["zone2"] or this.zone2
The fact is it's a way to access variables that doesn't exist in lot of other commonly used languages, in opposition to the way Ned Murphy suggested. So I can only agree with him and suggest you the same thing.
Now I understand the logic behind it and I've applied it, however, it's coming up with 'zone' as an undefined property? Very strange?