views:

746

answers:

2

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?

+8  A: 

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.

richleland
A: 

Use is keyword as richleland suggested.

Apologies in advance for nitpicking but I couldn't resist saying that:

  • getChildAt returns DisplayObject, not DisplayObjectContainer as you suggested in the question.
  • numChildren is a property of DisplayObjectContainer class and MovieClip is not the only derived class of it. Loader, Stage and Sprite extends DisplayObjectContainer. MovieClip is a subclass of Sprite. Hence numChildren trick will fail if you wanted to use movieclip specific actions like gotoAndStop on the child.
  • Proper way of comparing the name would be child.name == "specificName" unless you are using "specificName" as a prefix or suffix for all the children that are movieclips.
Amarghosh