Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flash as3 remove all children

Isn't there a simple "remove all children" function in flash? I don't understand why this code isn't working. I add children via:

for (var i in project_array[cp].project_type_clips){
        container.header.type_loader.addChildAt(project_array[cp].project_type_clips[i],i);
        loadCount++
    }

and then remove them via:

for (var i in project_array[cp].project_type_clips){
        container.header.type_loader.removeChildAt(i);
    }

But I get an error that the supplied index is out of bounds, and yet one clip is still left on stage. Likewise, if I try to add them without levels, like this:

for (var i in project_array[cp].project_type_clips){
        container.header.type_loader.addChild(project_array[cp].project_type_clips[i]);
        loadCount++
    }

and remove:

for (var i in project_array[cp].project_type_clips){
        container.header.type_loader.removeChild(project_array[cp].project_type_clips[i]);
    }

I get the same error.

like image 626
mheavers Avatar asked Feb 22 '11 19:02

mheavers


3 Answers

Yet another RemoveAllChildren loop:

while (container.numChildren > 0) {
    container.removeChildAt(0);
}
like image 178
splash Avatar answered Oct 19 '22 20:10

splash


When you remove an object, the childIndex of the other children is altered. Therefore, you cannot remove children using an increasing value for i, but have to start at numChildren-1 and then decrease:

for (var i:int = obj.numChildren-1; i >= 0; i--) {
   obj.removeChildAt (i);
}

should work.

like image 7
weltraumpirat Avatar answered Oct 19 '22 19:10

weltraumpirat


sprite.removeChildren(); removes all children as documented here.

like image 6
Narek Avatar answered Oct 19 '22 18:10

Narek