views:

3642

answers:

4

I want the mc to be removed with All its timers, events,... Is there a simple way to do this?

+5  A: 

Sure... unregister its timers and events, remove it from its parent, and delete all references to it. ;)

That is, AS3 is a garbage collected language, so you can't control when an object (including a MC) is really deleted from memory. When you delete all references to it (including event registration), the GC will detect that it is no longer needed and clean it up for you.

One thing you can (and should) do is, whenever you register for events, set the useWeakReference parameter to true. That is:

myMC.addEventListener( Event.ENTER_FRAME, onFrame, false, 0, true );

This tells AS3 that this particular event listener should not count as a reference - so if you remove all other references to the MC, it will get garbage collected even if you don't unregister the listener. (As for timers, if you're using setTimeout or setInterval, as far as I know they aren't considered references either. But I'm not sure about that.)

So the long and the short of it is, there is no way to nuke your MC and get Flash to clean up everything. That's just how AS3 works - managing your references and events is part of conscientious coding. If your MC has no more timers or events, is removed from its parent, and is not referenced by any part of your code, that's when it's gone.

fenomas
+1  A: 

Loader.unloadAndStop()

quoted from http://www.gskinner.com/blog/archives/2008/07/additional_info.html :

Here is the list of things that unloadAndStop does to prepare loaded SWFs for collection:

  • Stops all MovieClips
  • Stops all sounds playing/streaming
  • Stop/removes all Timer objects
  • Remove all global listeners for enterFrame, exitFrame, frameconstructed, activate, deactivate
  • Remove all stage listeners that have been created by the child.
  • Closes all NetConnection/NetStream
  • Video.attachNetStream/attachCamera(0)
  • Microphone.setLoopback(0)
  • Removes AS3 fonts from the global font table
  • Stops sockets, xmlsockets, filereference downloads, other downloading objects (grandchildren SWF), etc.
  • Frees bitmap related to cacheAsBitmap/filter, etc.

Note that all of the above are run recursively, so they will also apply to any nested children, including other loaded SWFs.

Unreality
Note that this is only for loaded swfs, not for MovieClips.
Cay
+1  A: 

I have seen some events, particularly Timer events, fail to fire when useWeakReference is set to true. So, be careful with fenomas's suggestion. I generally create a dealloc function in my MovieClips and addEventListener(Event.REMOVED_FROM_STAGE, dealloc); The dealloc function will fire when you removeChild() that MovieClip.

It's good practice to take care of this stuff yourself with Flash, especially when there are inconsistencies that crop up more often than we'd like.

Jeremy White
+1  A: 

Jeremy, to prevent your problem of events not being fired, just be sure that your item has a reference somewhere.

E.G. I made a preloader class which is called from the root timeline at frame 1 with command :

new PreLoader(root);

At this point there is no external reference to the PreLoader, and any _rootClip.loaderInfo.addEventListener(ProgressEvent.PROGRESS,fn) would fail. (_rootClip is a property of my PreLoader class and its the root passed as parameter).

What I did is simple : since I make a movie clip for the loading bar, and append this movieclip to the root, this movieclip has a reference. I just do myMC.preloader = this; to reference my preloader and prevent my events from deletion. As soon as I'm done with preloading, a simple _rootClip.removeChild(myMC); removes everything :)

Nirahiel