Looping through dynamic arrays in AS3

Just a wee silly thing here really, if you’re wanting to loop through incremental variables or arrays in AS3, occasionally you may get distracted (rightly so) by typecasting them, but you can crank out the old this[whatever] reference as well if you’re having no luck!

So say I have a bunch of arrays,

var Array0:Array = [val1,val2];
var Array1:Array = [val1,val2];
var Array2:Array = [val1,val2];

Then want to loop through them with a loop:

for(var i:uint = 0;i < numArrays;i++) 
{
  trace(currentArray);
}

You can reference the arrays dynamically by going

this["Array" + i];

So you’d be

for(var i:uint = 0;i < numArrays;i++) 
{
  trace(this["Array" + i]);
}

There’re other ways to do this, putting all your arrays into a master array and the like – but this’ll work if this is how you’re wanting to do it.

Tech Reference:

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑