Loop thru similarly named variables

OK, I’ll probably get flamed a bit for this because I know I could find related threads if I do a search. But I have found answers to related questions confusing. So here goes…

I have a “reset” function that removes MCs called head01, head02, head03,… head20. The way I’ve done it is as follows, because I don’t know how to loop through it:


if (head01) {
    if (head01.parent != null) {
     removeChild(head01);
    }
}
if (head02) {
    if (head02.parent != null) {
     removeChild(head02);
    }
}
if (head03) {
    if (head03.parent != null) {
     removeChild(head03);
    }
}
 
// etc. through head20.

How do I do this in a loop?

Thanks!
Tim

for (var i:int=1;true;i++) {
    var childName:String="head"+(i.toString().length==1?"0"+i:i);
    var child:DisplayObject=this.[childName];//this.getChildByName(childName);
    if (!child) break;
    if (!child.parent) removeChild(child);
}

}

[quote=Felixz;2326357]ActionScript Code:
[LEFT][COLOR=#0000ff]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000]var[/COLOR] i:[COLOR=#0000ff]int[/COLOR]=[COLOR=#000080]1[/COLOR];true;i++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]</p>
<p> [COLOR=#000000]var[/COLOR] childName:[COLOR=#0000ff]String[/COLOR]=[COLOR=#ff0000]“head”[/COLOR]+COLOR=#000000[/COLOR];</p>
<p> [COLOR=#000000]var[/COLOR] child:DisplayObject=[COLOR=#0000ff]this[/COLOR].[COLOR=#000000][[/COLOR]childName[COLOR=#000000]][/COLOR];[COLOR=#808080]//this.getChildByName(childName);</p>[/COLOR]
<p> [COLOR=#0000ff]if[/COLOR] COLOR=#000000[/COLOR] [COLOR=#0000ff]break[/COLOR];</p>
<p> [COLOR=#0000ff]if[/COLOR] COLOR=#000000[/COLOR] removeChildCOLOR=#000000[/COLOR];</p>
<p>[COLOR=#000000]}[/COLOR][/LEFT]

[/quote]

Thanks Feliz! I had a feeling you might be the one responding to my post.

I incorporated this without really understanding all the elements. (If you’re so inclined, I would appreciate a line-by-line explanation.) I did get the following error referencing the line containing “[COLOR=#000000]var[/COLOR] child:DisplayObject=[COLOR=#0000ff]this[/COLOR].[COLOR=#000000][[/COLOR]childName[COLOR=#000000]][/COLOR];”:

1084: Syntax error: expecting identifier before leftbracket.

Any help with that?

remove that dot
and if this won’t work either try commented code

[quote=Felixz;2326816]remove that dot
and if this won’t work either try commented code[/quote]

Neither works on my end.

Are head clips have set name property?

No. They’re all created like this:


private var head0x:Sprite;
addChild(head0x);
etc.

hey tpann

you could either set the names of the sprites:

head0x.name = "head0x";

then Felixz’s should work

or

you could create an array with references directly to all the heads and delete them there:


private var headArr:Array = new Array();
private var head0x:Sprite;
addChild(head0x);
headArr.push(head0x);
etc.[COLOR=#000000][COLOR=#007700][/COLOR][/COLOR]

and then to delete


for(var i = 0; i < headArr.length; i++){
   this.removeChild(headArr*;
}

hth