Be a Supporter!

Counting Children AS3

  • 4,554 Views
  • 7 Replies
New Topic Respond to this Topic
Xoria7
Xoria7
  • Member since: Jul. 7, 2009
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Counting Children AS3 2009-08-24 12:02:16 Reply

How do I make my code both count ALL the movieclips on stage, AND the amount of a certain type of movieclip on stage (Like ONE movieclip that's been added twice.)?


You could die after reading this. Get ready. Vid 1 ::::: Vid 2 ::::: My Site :::::

BBS Signature
Xoria7
Xoria7
  • Member since: Jul. 7, 2009
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to Counting Children AS3 2009-08-24 12:18:40 Reply

BBs is fast today. Bump


You could die after reading this. Get ready. Vid 1 ::::: Vid 2 ::::: My Site :::::

BBS Signature
GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to Counting Children AS3 2009-08-24 12:31:42 Reply

At 8/24/09 12:02 PM, Xoria7 wrote: How do I make my code both count ALL the movieclips on stage, AND the amount of a certain type of movieclip on stage (Like ONE movieclip that's been added twice.)?

mc.numChildren returns all of the display objects of a movieClip

to get a certain type the movieClips have to have tbeen added dynamically by giving them an export class, which I'm assuming you did. lets say the class is HotDog, you would say

var hotDogCount:int = 0;
for(var i:int = 0; i < mc.numChildren; i++){
var disObj:DisplayObject = mc.getChildAt(i);
if(disObj is HotDog)
hotDogCount++;
}

another interesting thing if you trace disObj.toString() you get something like "[Object HotDog]" so what like to do to uniquely name them is take off the first 8 characters and the last one and add hotDogCount to it when it is generated. then when you want to cycle through you can say
mc.getChildByName("HotDog" + X);//where X < hotDogCount

mike
mike
  • Member since: Feb. 24, 2000
  • Offline.
Forum Stats
Member
Level 20
Programmer
Response to Counting Children AS3 2009-08-24 12:34:01 Reply

Try something like this:

function countChildren(clip:DisplayObjectContainer):void
{
  var count:uint = numChildren;
  for(var i:uint=0; i<count; i++)
  {
     var child:DisplayObject = getChildAt(i);
     if(child is DisplayObjectContainer)
       count += countChildren( DisplayObjectContainer(child) );
  }
}

countChildren(root);

To count instances of a particular type of clip, just check if(child is MySpecialClip)

good luck!

Xoria7
Xoria7
  • Member since: Jul. 7, 2009
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to Counting Children AS3 2009-08-24 12:53:18 Reply

Thanks!


You could die after reading this. Get ready. Vid 1 ::::: Vid 2 ::::: My Site :::::

BBS Signature
Xoria7
Xoria7
  • Member since: Jul. 7, 2009
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to Counting Children AS3 2009-08-24 14:46:24 Reply

Uhh, I tried to copy, but not much success. It won't work!

var mc:mov = new mov();

var Element2Count:int = 0;
for(var Element2var:int = 0; Element2var < mc.numChildren; Element2var++){
var disObj:DisplayObject = mc.getChildAt(Element2var);
if(disObj == mc) {
Element2Count++;
}

}

addChild(mc);
trace ("Element2Count is " + Element2Count);

You could die after reading this. Get ready. Vid 1 ::::: Vid 2 ::::: My Site :::::

BBS Signature
GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to Counting Children AS3 2009-08-24 14:54:48 Reply

At 8/24/09 02:46 PM, Xoria7 wrote: Uhh, I tried to copy, but not much success. It won't work!

var mc:mov = new mov();

im assuming mov is a custom class you made?

var Element2Count:int = 0;
for(var Element2var:int = 0; Element2var < mc.numChildren; Element2var++){
var disObj:DisplayObject = mc.getChildAt(Element2var);
if(disObj == mc) {

if(disObj is mov)
the is operator checks if an object to the left is an instance of the class to the right. if you don't know how to make custom classes then you should look for a tutorial, it's arguably one of the most important features of flash or any object oreinted language

Element2Count++;
}

}

addChild(mc);
trace ("Element2Count is " + Element2Count);
GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to Counting Children AS3 2009-08-24 15:00:18 Reply

perhaps i should have broken down my code more

var hotDogCount:int = 0;
for(var i:int = 0; i < mc.numChildren; i++){
//mc is the movieclip with the children you are looking for, if they are not inside a movieclip then just say numChildren or this.numChildren(preferably the first)
var disObj:DisplayObject = mc.getChildAt(i);
if(disObj is HotDog)
"is" returns true if disObj is a HotDog meaning it was loaded into the flash by saying new HotDog(). otherwise it returns false
hotDogCount++;
}