views:

164

answers:

2

I am loading/unloading several swfs from one main swf. When I load a new swf I do something like this:

contentContainer.addChild(the new swf); //add the new swf
contentContainer.swapChildrenAt(0,1);
contentContainer.removeChildAt(1); //remove the previous swf

My question is, when I removeChildAt(), does the old swf keep "playing" and keep taking up cpu resources? How can I kill it completely? If there is audio or video in the old swf, it seems to keep playing even after it is removed.

A: 

You might want to try something like:

var mc:MovieClip = contentContainer.removeChildAt(1) as MovieClip;
mc.stop();

or make some public stop() methods on the movies embedded in the unloaded swf. (ref)

You'll probably need some variation on this.

Also, if you are able, you could also have the child swfs listen for the Event.ADDED_TO_STAGE, Event.REMOVED_FROM_STAGE events. It might make more sense to have them control themselves in this sense rather than relying on the parent container to stop them.

Glenn
Listening for REMOVED_FROM_STAGE is a good solution. Thanks.
sol
This doesn't actually answer the question though. Even if you remove MCs from the stage and stop them, they will continue to take up memory until they are garbage collected. The only answer here is to keep track of your references and make sure your clip will get GCed correctly.
fenomas
The question is CPU resources. Not memory. And AFAIK, there's no way to invoke GC, except by clearing all variable references and crossing fingers.
Glenn
+1  A: 

try with Loader.unloadAndStop()

Cay
Loader.unloadAndStop is definitely what you need as long as you're deploying to Flash Player 10 (or greater)
Luke Bayes