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