Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify MovieClip in AS3 among DisplayObjectContainer

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?

like image 797
Raptor Avatar asked Nov 20 '25 02:11

Raptor


2 Answers

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.

like image 172
richleland Avatar answered Nov 23 '25 01:11

richleland


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.
like image 40
Amarghosh Avatar answered Nov 23 '25 02:11

Amarghosh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!