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.
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);
}
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.
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*;
}