Drahcir
Drahcir

Reputation: 11972

Loop through each instance of a graphic

What I have is a lot of buttons (over 200), and I want to loop through them all. They're all instances of the same graphic symbol in the IDE, so there should be a way to loop through them all.

If I was doing this or something similar in JavaScript, I could do something like: document.getElementsByClassName('MyGraphicSymbol') and then I'd have an array of all the elements. So looking for something like this in AS3.

Upvotes: 0

Views: 428

Answers (3)

apscience
apscience

Reputation: 7253

Have you thought about a static array for the class, and in the constructor add the new instance to that array. Then you can just do

for each(var i in YourButtonClass.arrayOfButtons){
//do nothing
}

Upvotes: 1

crashbus
crashbus

Reputation: 1714

All classes and objects in actionscript 3 are build internal as associativ array. This means you can get an element e.g. like this:

this ["instanceName"];

When every button has an instance name like "button_" + i you can iterate over all buttons with that internal array:

for (var i= 0; i < 200; i++){
var button: SimpleButton = this ["button_" + i] as SimpleButton;
// do something
}

Upvotes: 1

Mattias
Mattias

Reputation: 3907

I always add my items to a Vector or Array to better have control over them. But ... if you have added all buttons to the same container you could do this:

var buttons : int = buttonHolder.numChildren;
var button : MovieClip;

for(var i : int = 0 ; i < buttons ; i++)
{
    button = buttonHolder.getChildAt(i);
    button.someFunctionOfChoice();
}

Upvotes: 1

Related Questions