Identify MovieClip in AS3 among DisplayObjectContainer

Raptor picture Raptor · Oct 30, 2009 · Viewed 7.3k times · Source

in ActionScript 3, if I loop through the children of a movie clip, it will return a DisplayObjectContainer, which is a list of DisplayObjects.

However, the AS3 typeof cannot identify MovieClip as MovieClip is now an object, instead of a data type. How can I correctly identify MovieClip?

I found 3 solutions online:

Solution 1 (the solution I am using):

First set the MovieClip name to a specific name, then in the iterate process, check the name of children using child.name.indexOf("specificName") > -1

Solution 2:

use child.hasOwnProperty("numChildren") to identify a MovieClip

Solution 3:

use 3rd party plug-in like FlashDevelop

which solution is the best? or is there any alternatives?

Answer

richleland picture richleland · Oct 30, 2009

It's actually much improved and simplified in AS3. You can simply use the "is" operator:

for(var i:int = 0; i < containerObj.numChildren; i++) {
    if(containerObj.getChildAt(i) is MovieClip) {
        // do something
    }
}

The Flash livedocs for this topic have some more detail.