00:00
00:00
Newgrounds Background Image Theme

Thrll just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Remove Everything

2,144 Views | 13 Replies
New Topic Respond to this Topic

Remove Everything 2010-05-29 20:22:03


AS3: MAIN

This is a recursive algorithm you can use to remove everything nested inside a MovieClip hierarchy.
It's all very self explanatory. The 'removeEverything' function takes a MovieClip and for every child which is also MovieClip, calls itself, passing the child as the argument. The 'removeAllChildren' function simply removes every child of its argument MovieClip. This gets called only when every call to 'removeEverything' has returned.

//Remove Everything
function removeEverything(mc:MovieClip)
{
	for(var i:int = mc.numChildren-1; i >=0; i--)
	{
			if(getClass(mc.getChildAt(i)) == MovieClip)
			{
				removeEverything(MovieClip(mc.getChildAt(i)));
			}
	}
	removeAllChildren(mc);
}

//Remove All Children
function removeAllChildren(mc:MovieClip)
{
	while(mc.numChildren)
	{
		mc.removeChild(MovieClip(mc).getChildAt(0));
	}
}

//Get Class
function getClass(obj:Object):Class 
{   
	return Class(getDefinitionByName(getQualifiedClassName(obj)));   
}

Response to Remove Everything 2010-05-29 20:27:59


the whole getClass thing isn't needed - use

if (myInstance is MovieClip) { }

instead. Also, This won't work if you have added any Sprite to your displaylist.

Finally, when ever would you need such a function?

Response to Remove Everything 2010-05-29 20:33:59


No need to use class equality, simply use the 'is' operator. Also, what if its a sprite and not a movieclip? Sprites can hold things too. A better solution:

if( mc.getChildAt(i) is DisplayObjectContainer ){
    //stuff
}

#include <stdio.h>

char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";

main() {printf(p,10,34,p,34,10);}

BBS Signature

Response to Remove Everything 2010-05-29 20:46:41


Oh and the code is twice as slow as it needs to be. In the first for loop you should just remove it there. In general better code overall:

function removeChildren(mc:DisplayObjectContainer) {
    for(var i:int = mc.numChildren-1; i >=0; i--) {
        var child:* = mc.getChildAt(i);
        if(child is DisplayObjectContainer) {
            removeChildren(mc.getChildAt(i));
        }
        mc.removeChild(child);
    }
}

Far cleaner, faster, flexible, and uses 1 single recursive function.


#include <stdio.h>

char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";

main() {printf(p,10,34,p,34,10);}

BBS Signature

Response to Remove Everything 2010-05-29 20:48:12


Sorry for triple post:
removeChildren(mc.getChildAt(i));
should be
removeChildren(child);


#include <stdio.h>

char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";

main() {printf(p,10,34,p,34,10);}

BBS Signature

Response to Remove Everything 2010-05-29 21:07:07


Oh wow, how did I not think of that? I learn something new everyday.

Response to Remove Everything 2010-05-30 02:45:48


I'm using this:

public function _removeMovieClip (target:Object,_clip:Sprite):void {
		_clip.graphics.clear ();
		while (_clip.numChildren > 0) {
			_clip.removeChildAt (0);
		}
		target.removeChild (_clip);
	}

_removeMovieClip(this,racecar);

Response to Remove Everything 2010-05-30 04:22:31


why would you ever need to unhook everything recursively like that?

Response to Remove Everything 2010-05-30 07:46:03


At 5/30/10 04:22 AM, Glaiel-Gamer wrote: why would you ever need to unhook everything recursively like that?

I was under the impression that if you just remove a DisplayObject which has children, those children still exist in memory.

I use this when closing a menu, for example.

Response to Remove Everything 2010-05-30 07:58:10


Nah that's the beauty of putting things in a container clip. You just remove the container and that's it. Eventlisteners are the stuff that you wanna remove though.


You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.

Response to Remove Everything 2010-05-30 08:02:03


At 5/30/10 07:58 AM, ProfessorFlash wrote: Nah that's the beauty of putting things in a container clip. You just remove the container and that's it. Eventlisteners are the stuff that you wanna remove though.

Sweet tits. But even if I set useWeakRefernce of all my listeners to true, don't they still stcik around if I just remove the parent display object?

Response to Remove Everything 2010-05-30 08:18:19


It seems like I had tried to remove the container, but things were still messed up, but then again I had used line graphics and using .clear() fixed that finally and I must have stuck with the recursion though.

Still learning as3 as I go.

Response to Remove Everything 2010-05-30 15:04:34


At 5/30/10 08:02 AM, Drakenflight wrote: Sweet tits. But even if I set useWeakRefernce of all my listeners to true, don't they still stcik around if I just remove the parent display object?

The simple fix is to not use weak references and actually remove your listeners when you are done with them.

Then again, I never got the point of weak references anyway. It's pretty easy to copy/paste an addEventListener and change it to removeEventListener. Alot easier than adding all that extra crap at the end of your listener, and then wondering exactly when the event listener is going to be GCed anyway.

Response to Remove Everything 2010-05-30 16:10:20


The point of an listener weak reference is that it won't protect the object that is listening against garbage collection. The garbage collector will only garbage collect objects once they become completely unreachable (nothing has a reference to it) event listeners listeners also count as references if weak reference is not enabled.

I personally never use them, because I prefer having complete control over the garbage collection process, and what is listening to what events at any given time.


#include <stdio.h>

char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";

main() {printf(p,10,34,p,34,10);}

BBS Signature