Use an objects name to make changes to the object

51 Views Asked by At

I have created multiple instances of one object each with there own instance names with numbers on the end. I would like to iterate through these names (they have numbers on the end), modify there properties then push them to an array all through the instance names not the actual object instances.

if (instance60.currentFrame == 4)
{
    array.push("instance60")
}

Where the first instance60 is refering to the object itself and the second instance60 is just a pushed string

1

There are 1 best solutions below

0
Anthony Pace On

I think what you are looking to do is set a key value pair.

The key would be how you find the reference to the object later

instances["instance"+60] = instance60;

you can loop using something like:

for(var i:uint=0;i<instances.length;i++){
   instances["instance"+i] = new Sprite();
   instances["instance"+i].x = 20;
   instances["instance"+i].y = 20*i;    
   //etc...
}

But, although this has it's uses, in this case you are iterating through an integer value anyways, so, given it can run quite a bit faster on large arrays, why not just store them at their position in the array?

for(var i:uint=0;i<instances.length;i++){
       instances[i] = new Sprite();
       instances[i].x = 20;
       instances[i].y = 20*i;
       this.addChild(instance[i]);    
       //etc...
    }