PDA

View Full Version : removeChild() and delete



Dan0
August 8th, 2007, 01:50 PM
Hi,
I have a large ProjectMenu class that is added to the display and contains many other classes. These have many events within each of them, such as rollOver, rollOut, enterFrame, mouseDown etc...
When a button is pressed for example, I want a ProjectGallery class to be created, fair enough. I also want the ProjectMenu to be removed from the display. Current, I have it so that when it is added events like stage.onResize, enterFrame are added, and when removed from display these events are removed. But also the classes within them have listeners that are not removed. The ProjectMenu class as a whole takes up quite a lot of CPU/Memory. Should I delete the instanceafter it is not being used, and re-create the instance when it is needed again, or will removeChild() be sufficient? I except the answer will be to use the delete keyword but I need some more info on what kinds of things will be using CPU/Memory.
Thanks for the help,
Dan :be:

-yeh ok i read it http://www.kirupa.com/forum/showthread.php?p=2129547#post2129547
-autoformat is killing me :( taking out every bleeding parenthesis

Dan0
August 8th, 2007, 01:56 PM
Heh, If I delete the instance will it kill all the event listeners? It better do *shakes fist*.

-
Meh, I guess its;

removeChild(projectMenu);
projectMenu = null;

-
Ok so It works if I do this, however, if I "don't" tell it to remove say, the enterFrame event when it is removed from the stage, the event contineous running, even when it's ONLY identifying variable is defined as null. Does this mean that the events within the buttons inside ProjectMenu will still be listening even though they can't be triggered?

senocular
August 8th, 2007, 02:32 PM
You would have to remove the event listeners too.

Best practice is to set up event listeners in ADDED_TO_STAGE and remove them in REMOVED_FROM_STAGE (unless they're persistent)

Dan0
August 8th, 2007, 02:44 PM
Yes that is exactly what I have done. But what about all the buttons/other instances displayed inside of the projectMenu instance? Do I have to set up ADD_TO/REMOVE_FROM_STAGE events in each of them?

senocular
August 8th, 2007, 02:52 PM
ideally, yes. But the kind of events that depend on an object being on screen (rollover, click, etc) are not as important since they will be cleaned up when the object itself is cleaned up from memory. And, really, its even better still if you use addEventListener with weak listeners which will help this cleanup process.

For more information on that, read grant skinners article on garbage collection (dont have the link handy but you can easily google it)

Dan0
August 8th, 2007, 05:33 PM
Right, thanks, when you start thinking about it, it can boggle you.
You can remove it from sight but it still has a MOUSE_DOWN event which can't be triggered because you can't see it, so it has no hitbox and when you remove it from being identified it still has the event, which can't be triggered and even if it was triggered would not be able to call a function as the function's instance doesn't exist and can't be reached by anything anyway!
:ko:

How about though, if you have an instance, this is removed from the displayObject and identifiers, but has an enterFrame event still running, which has an identifier to a "known" instance. This "unknown" instance then calls a function on the "known" instance and parses a referenece of itself to the "known" instance. Does that mean it can be "re-referenced" again?
Resurection?! :d: