Marko
Marko

Reputation: 10992

How to find all objects of a type on the stage?

I'm trying to iterate through all the objects in the stage and I'm not sure how to do it. It's kind of improvised through my previous experience with C# and javascript.

  1. First I need the correct list/array with all the stages children.
  2. I need to check their type. I have a special custom class which extends Sprite with some additional properties only.

Someone proficient in actionscript 3.0 who can show the proper way to do?

for(var obj:DisplayObject in DisplayObjectContainer) {
    if(typeof obj == "Pic") {

Upvotes: 1

Views: 5832

Answers (2)

Max Dohme
Max Dohme

Reputation: 700

The easiest would be to use the "is" operator to accertain the object's class.

An example:

for( var i:int = stage.numChildren - 1; i>=0; i-- ) {
    if( stage.getChildAt(i) is Pic ) {
        // Do stuff with members of Pic class

Upvotes: 4

I don't think you can get to the children of a DisplayObjectContainer like that. You might need to do this:

for(var i=0;i<container.numChildren;i++)
{
     if(container.getChildAt(i) is Pic) doSomething();
}

where container is a DisplayObjectContainer.

Upvotes: 2

Related Questions