In this small part of action script event handling we will learn about one of the effective event handling technique that can help us to dispose our objects towards garbage collection( I will be writing about this interesting topic soon.)- in other way we can make efficient application without causing any memory leaking. The rule of thumb is to make use of removeListener and remove any event listener attached to some object before you remove or delete it by either removeChild or delete method.
When ever we create objects and attach events with them, events go on executing if that object resides in memory. Let me give you one small example:
Create a movieClip instance on the stage and give it a name ( foo_mc in my example). Now on the first frame open action panel and write down code for ENTER_FRAME event of that movielClip and then remove the foo_mc. The example code is given below:
-
foo_mc.addEventListener(Event.ENTER_FRAME, trackObject);
-
-
function trackObject(evt:Event){
-
removeChild(foo_mc);
-
trace("I am executing");
-
}
When you execute this code, even if you have removed the child by removeChild method, it will not be removed from memory and trackObject function will be executing. Even if you add foo_mc=null, it will still execute the function. The reason is foo_mc is not deleted completely from memory at this point of time. It may be removed latter some time when garbage collector will see foo_mc does not have any reference to other objects. Here this loop of enter frame will go on because foo_mc has a listener to the enter frame event. One thing you should always remember that removeChild() never removes display objects from memory, it only removes from it’s parents display list. If this situation arises then it’s always wise to remove the event listener attached to it before you remove it from the display list. Also it’s a good programming practice to make the methods of a n object to null when not required in future so that they won’t eat up some memory. This case grow bigger when there are conditions for looping. So a better code may look like this:
-
foo_mc.addEventListener(Event.ENTER_FRAME, trackObject);
-
-
function trackObject(evt:Event){
-
foo_mc.removeEventListener(Event.ENTER_FRAME, trackObject);
-
removeChild(foo_mc);
-
trace("I am executing");
-
}
If you feel some thing different about this, please share with me.

hi I’m looking for a tutorial for a very complex mcButton. I have an MC “homeBtn” that I’m using as an animated button.
The “homeBtn” is made of 2 mirroring MC’s named “topMC” and “bottomMC”.
Inside “topMC” is the animation named “liquidMC”
I am having diffculty running them. Works great in my mind LOL. Let me know. I have the code of what I have so far.