Recursive Child Kill
Tuesday, October 7th, 2008I was reading a lot about Garbage Collection, AS2->AS3 RemoveMovieClip, and such. People are all complaining about having to be precise with GC. It is not that big a deal but it got me to wondering. So I started to play with different recursive functions to remove clips and such. This is a start that I will continue to work on and improve.
// To tell the difference between Shape / MovieClip / ... function checkObjectType(object:Object, type:String = 'Shape'):Boolean{ if ( object.toString() == '[object '+type+']') return true; return false; } // recursive function to sweep through children of a movieclip // to kill off shapes, text, movieclips, sprites, ... Object.prototype.sweep = function(){ for( var i:int=0; i<this.numChildren; i++){ var childClip = this.getChildAt(i); if(checkObjectType( childClip, 'MovieClip' )) childClip.sweep(); else this.removeChildAt(i); } // we use 0 in the iteration because display list will // shift down after we remove an item. // so we are always taking out the bottom of the list for( i=0; i< this.numChildren; i++) this.removeChildAt(0); } // usage var rootClip = getChildAt(0); rootClip.sweep();
I will be doing more work with this and seeing how far I can actually push the ease of GC.




