# Cycling through displaylist recursivly

**URL:** <https://forum.kirupa.com/t/cycling-through-displaylist-recursivly/263912>\
**Category:** flash\
**Created:** [June 20, 2008, 11:34am UTC](https://forum.kirupa.com/t/cycling-through-displaylist-recursivly/263912 "2008-06-20T11:34:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zoimt](https://avatars.discourse-cdn.com/v4/letter/z/f08c70/32.png) [@zoimt](https://forum.kirupa.com/u/zoimt)\
**Post date:** [June 20, 2008, 11:34am UTC](https://forum.kirupa.com/t/cycling-through-displaylist-recursivly/263912/1 "2008-06-20T11:34:03Z")

</div>

First, let me take this line to say hello, since I new around here after beeing directed to this forum by my actionscript teacher and classmates.  
Hello there!  
I am kinda new to Actionscript and while working on my first project I struggle with the following:  
What I want to do is just cycling through the display list, check if the instance name of an MovieClip matches the filter String and then returns an Array of those MC’s.

The code goes like this:

```php

var buttonArray:Array = makeArray(this, "button"); // get buttons from DisplayList

function makeArray(mc:MovieClip, filter:String) {
// Cycle through the display list, return an array of all MovieClips matching the filter string
	var array:Array = new Array();

	for(var i:int = 0; i < mc.numChildren; i++) {
		trace(mc.getChildAt(i).name.substring(0, filter.length)); //test
		if(mc is MovieClip)
		{
			if(mc.getChildAt(i).name.substring(0, filter.length) == filter) {
			/** Tests if childs name includes button. cant figure out why i must check for 
			    7 character while only searching for six. Maybe they start String.substring 
				with 1 in AS, unlike Java? **/
			
				array.push(mc.getChildAt(i));
			}
			else if(mc.getChildAt(i).numChildren > 0) {
				array.concat(makeArray(mc.getChildAt(i), filter));
			}
		}
		return array;
	}
}

```

Compiletime Errors:  
1119: Access of possibly undefined property numChildren through a reference with static type flash.display:DisplayObject. @ else if(mc.getChildAt(i).numChildren \> 0) {

1118: Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type flash.display:MovieClip. @ array.concat(makeArray(mc.getChildAt(i), filter));

I kinda dont get the reason for this. If I test the Displayobject to be a MovieClip, explicitly, why do I reference a _possibly_ undefined property? I thought about the possibility that the Child of an MC might not be an instance of MC, like a StaticText object or whatever, but since i check the instance to be an MC in the first place, I can’t figure out the problem anymore.

Regards,  
Zoimt

---

<div class="post-metadata">

**Author:** ![ZaharijePasalic](https://avatars.discourse-cdn.com/v4/letter/z/c2a13f/32.png) [@ZaharijePasalic](https://forum.kirupa.com/u/ZaharijePasalic)\
**Post date:** [June 20, 2008, 2:15pm UTC](https://forum.kirupa.com/t/cycling-through-displaylist-recursivly/263912/2 "2008-06-20T14:15:03Z")

</div>

- Error 1119: getChildAt will return DisplayObject which does not have numChildren. To resolve this, firstly check returned child if it is DisplayObjectContainer than check numChildren.
- Error 1118: again, getChildAt will return DisplayObject, and recursive call expect MovieClip

Here is my code:

```auto
 
trace(makeArray(this, "button").length); 
 
function makeArray(dp:DisplayObjectContainer, filter:String) : Array {
   var retResult : Array = new Array();
   for (var i:int=0; i<dp.numChildren; i++) {
      var child = dp.getChildAt(i);
      if (child.name == filter)
         retResult.push(child);
      if (child is DisplayObjectContainer) 
         retResult.concat(makeArray(child as DisplayObjectContainer, filter)); 
   }
   return retResult;
}

```

Best.

---

<div class="post-metadata">

**Author:** ![zoimt](https://avatars.discourse-cdn.com/v4/letter/z/f08c70/32.png) [@zoimt](https://forum.kirupa.com/u/zoimt)\
**Post date:** [June 20, 2008, 6:47pm UTC](https://forum.kirupa.com/t/cycling-through-displaylist-recursivly/263912/3 "2008-06-20T18:47:18Z")

</div>

Quick edit: works!  
Just a second ago nothing worked but it can all be traced back to the little error I made by using Array.concat. To use it properly, I better write

```php
retResult = retResult.concat(makeArray(child as DisplayObjectContainer, filter)); 

```

Thank you.

EDIT:: Oh so many thanks again! I’m crying for happiness at this moment, this will make everything so much easier from now on in this project!!! =)

---

<div class="post-metadata">

**Author:** ![ZaharijePasalic](https://avatars.discourse-cdn.com/v4/letter/z/c2a13f/32.png) [@ZaharijePasalic](https://forum.kirupa.com/u/ZaharijePasalic)\
**Post date:** [June 20, 2008, 7:52pm UTC](https://forum.kirupa.com/t/cycling-through-displaylist-recursivly/263912/4 "2008-06-20T19:52:49Z")

</div>

Glad to help.

Best.
