# Loop through children?

**URL:** <https://forum.kirupa.com/t/loop-through-children/264695>\
**Category:** flash\
**Created:** [June 28, 2008, 10:56pm UTC](https://forum.kirupa.com/t/loop-through-children/264695 "2008-06-28T22:56:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DesignerMichael](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@DesignerMichael](https://forum.kirupa.com/u/DesignerMichael)\
**Post date:** [June 28, 2008, 10:56pm UTC](https://forum.kirupa.com/t/loop-through-children/264695/1 "2008-06-28T22:56:06Z")

</div>

Is it possible to loop through all of the children in a class that you have added to stage?

Eg: myNewClass:testClass = new testClass()  
addChild(myNewClass);

Then… Something like…  
for (myVar:testClass in stage){  
}

Anyone have any ideas? For now I’m just naming each child and putting the names in an array, and using getChildByName as I go through the array with a for loop.

---

<div class="post-metadata">

**Author:** ![scallywag](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@scallywag](https://forum.kirupa.com/u/scallywag)\
**Post date:** [June 28, 2008, 11:17pm UTC](https://forum.kirupa.com/t/loop-through-children/264695/2 "2008-06-28T23:17:08Z")

</div>

[quote=DesignerMichael;2350798]Is it possible to loop through all of the children in a class that you have added to stage?

Eg: myNewClass:testClass = new testClass()  
addChild(myNewClass);

Then… Something like…  
for (myVar:testClass in stage){  
}

Anyone have any ideas? For now I’m just naming each child and putting the names in an array, and using getChildByName as I go through the array with a for loop.[/quote]  
maybe like this

```auto
for (var i:int=0; i < containerName.numChildren; i++) {
 trace(containerName.getChildAt(i).toString( ));
}

```

---

<div class="post-metadata">

**Author:** ![DesignerMichael](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@DesignerMichael](https://forum.kirupa.com/u/DesignerMichael)\
**Post date:** [June 28, 2008, 11:25pm UTC](https://forum.kirupa.com/t/loop-through-children/264695/3 "2008-06-28T23:25:13Z")

</div>

Hmmm… I would have to put all of the players in MovieClip, and the projectiles in another.

I didn’t realize this. This makes handling depths alot easyier!

[quote=scallywag;2350804]maybe like this  
ActionScript Code:  
[LEFT][COLOR=#0000ff]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000] **var** [/COLOR] i:[COLOR=#0000ff]int[/COLOR]=[COLOR=#000080]0[/COLOR]; i \< containerName.[COLOR=#000080]numChildren[/COLOR]; i++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]  
[COLOR=#0000ff]trace[/COLOR][COLOR=#000000]([/COLOR]containerName.[COLOR=#000080]getChildAt[/COLOR]COLOR=#000000[/COLOR].[COLOR=#0000ff]toString[/COLOR][COLOR=#000000]([/COLOR] [COLOR=#000000])[/COLOR][COLOR=#000000])[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[/LEFT]

[/quote]
