In Part 1, I went over the “loop” callback function, and everything you need to know is there. However, I want to elaborate on the “done” callback function, as this is where external assets (e.g., movieclips, images, sound files, SWF files, etc) are added and removed from the preloader.

When the “loader” object has fully downloaded loadedmovie.swf, the event manager fires off the Event.COMPLETE event, and that causes the “loader” object to call the “done” function.

Recall that we have two assets in the display list of the main Timeline (i.e., movieClip) object:  the “arrows” and the “percent” objects. We need to remove those now because we want to load the loadedmovie.swf application.  We do this by making a call to the native DisplayObjectContainer method “removeChildAt():”

removeChildAt(getChildIndex(percent));
removeChildAt(getChildIndex(arrows));

This removes “arrows” and “percent” display objects from the display list of the instantiated DisplayObjectContainer object.

Again, what is the  instantiated DisplayObjectContainer object?

It is the SWF file itself, an instantiated movieClip class. This why the statements “percent.root” and   “arrrow.root” return “MainTimeline” as the container object.

Now, in addition to removing display objects from our container’s display list, we should also remove them from memory. This is because removing objects from the display list simply tells the display manager NOT to display the objects in a flash application – however, they still exist taking up memory in RAM. Most of the time, this is OK, but we know we don’t need these assets anymore, so let’s just get rid of them.

The way we do this is simple: we just set  “arrows” and “percent” to “null,” but – OK, it’s not really that simple.”

When we set a display objects to “null,” we schedule that object for "garbage collection."  Garbage collection is when the memory manager removes “objects” from system memory. However, object removal from memory is up to your operating system and the flash memory manager: they decide when – and if – to claim the memory. In short, garbage collection is not instantaneous when the flash runtime executes the statements that set “arrows” and “percent” to “null.”   

OK, so what’s the big deal?  The deal is that display objects in memory are still “running,” e.g., they can still receive events; if you’ve set up an interval event that triggers processing, that process will continue until the object is removed from memory.  Look inside the “circular arrows” movieclip. You’ll see that it is continually looping from frame 1 to 40. Even though we’ve removed the “arrows” object from the display list, so we can’t “see” it anymore, and we’ve set the “arrows” instance to “null,” if garbage collection doesn’t happen right away, not only does the “arrows” stay in RAM taking up memory, it also continues to loop taking up CPU resources. 

So, we need to make sure all processes have stopped inside the display objects before we schedule them for garbage collection. Here’s the full set of statements in our “done” function:

// 1st) remove each child from the display list
removeChildAt(getChildIndex(percent));
removeChildAt(getChildIndex(arrows));
// 2nd) disable any running code in any child
// (see chap. 14, Moock, "<a href="http://www.amazon.com/gp/product/0596526946?ie=UTF8&tag=broskydes-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0596526946">Essential ActionScript 3.0</a>" for more details)
arrows.gotoAndStop(1); // stop looping
// set instance references to null so the garbage collector can remove them from memory
percent = null;
arrows = null;