Be a Supporter!

Remove Everything

  • 1,118 Views
  • 13 Replies
New Topic Respond to this Topic
Drakenflight
Drakenflight
  • Member since: Feb. 23, 2010
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Remove Everything May. 29th, 2010 @ 08:22 PM Reply

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)));   
}
BasV
BasV
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 23
Game Developer
Response to Remove Everything May. 29th, 2010 @ 08:27 PM Reply

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?

Redshift
Redshift
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to Remove Everything May. 29th, 2010 @ 08:33 PM Reply

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
Redshift
Redshift
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to Remove Everything May. 29th, 2010 @ 08:46 PM Reply

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
Redshift
Redshift
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to Remove Everything May. 29th, 2010 @ 08:48 PM Reply

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
Drakenflight
Drakenflight
  • Member since: Feb. 23, 2010
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to Remove Everything May. 29th, 2010 @ 09:07 PM Reply

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

zenyara
zenyara
  • Member since: Jun. 17, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to Remove Everything May. 30th, 2010 @ 02:45 AM Reply

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);
Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to Remove Everything May. 30th, 2010 @ 04:22 AM Reply

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

Drakenflight
Drakenflight
  • Member since: Feb. 23, 2010
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to Remove Everything May. 30th, 2010 @ 07:46 AM Reply

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.

ProfessorFlash
ProfessorFlash
  • Member since: Oct. 6, 2007
  • Offline.
Forum Stats
Member
Level 32
Programmer
Response to Remove Everything May. 30th, 2010 @ 07:58 AM Reply

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.

Drakenflight
Drakenflight
  • Member since: Feb. 23, 2010
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to Remove Everything May. 30th, 2010 @ 08:02 AM Reply

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?

zenyara
zenyara
  • Member since: Jun. 17, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to Remove Everything May. 30th, 2010 @ 08:18 AM Reply

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.

Version2
Version2
  • Member since: Sep. 24, 2003
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to Remove Everything May. 30th, 2010 @ 03:04 PM Reply

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.

Redshift
Redshift
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to Remove Everything May. 30th, 2010 @ 04:10 PM Reply

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