PDA

View Full Version : Writing a reset function



snottlebocket
February 7th, 2009, 04:36 PM
Maybe I'm overlooking something really obvious here but is it possible to write a reset function in flash without keeping track and deleting all your assets and event listeners one by one in AS3?

In as2 I used to make sure everything was inside one all encompassing container movieclip which I ditched using removeMovieclip when it was no longer needed, then running my init function again.

But in AS3 you're supposed to use the removeChild function which only removes the container from the display list, leaving the game pretty much up and running albeit invisibe.

Is it possible to write a simple function that'll ditch the container and everything running inside or do I need to write an extensive function that'll stop every event broadcaster and listener as well as null all the movieclip references and such?

therobot
February 7th, 2009, 11:23 PM
You might try removeChild on your container and then set the container to null, which should open it up for garbage collection, but you still need to make sure you remove any listeners and references that would make flash believe the object is still in use.

snottlebocket
February 8th, 2009, 05:16 AM
Thanks, I did try the removeChild + null but I didn't remove all the internal listeners running within the container.

I'll give that a go.

substance
February 8th, 2009, 10:01 AM
Yes, you have to manually remove event listeners... unless


addEventListener () method
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void

useWeakReference:Boolean (default = false) — Determines whether the reference to the listener is strong or weak. A strong reference (the default) prevents your listener from being garbage-collected. A weak reference does not.
Class-level member functions are not subject to garbage collection, so you can set useWeakReference to true for class-level member functions without subjecting them to garbage collection. If you set useWeakReference to true for a listener that is a nested inner function, the function will be garbage-collected and no longer persistent. If you create references to the inner function (save it in another variable) then it is not garbage-collected and stays persistent.

therobot
February 8th, 2009, 12:57 PM
So what you're saying is as long as the listeners a class uses are set to a weak reference and no classes outside of it reference those listeners/functions, you don't need to manually remove the listeners? That's pretty nice.