Cycling through displaylist recursivly

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:


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

  • 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:

 
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.

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

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!!! =)

Glad to help.

Best.