I have a basic question about manually removing event listeners in actionscript 3.
If I have a function like:
private function doStuff(event : Event):void
{
//let the开发者_如何学JAVAm save
var f:FileReference = new FileReference();
f.addEventListener(Event.COMPLETE,saveDone);
f.save(mp3Encoder.mp3Data,"output.mp3");
}
How do I remove the event listeners when the saveDone function is called? Normally I just change the "add" to "remove" like:
f.removeEventListener(Event.COMPLETE,saveDone);
However, f is a local variable, and I can't get to it after the doStuff function ends.
private function saveDone(ev:Event){
f.removeEventListener(Event.COMPLETE,saveDone);
}
maybe try to reference the original object via the "target" property of the event? I haven't tried it, but something akin to:
private function saveDone(ev:Event){
var originalFR:FileReference = ev.target as FileReference;
originalFR.removeEventListener(Event.COMPLETE, saveDone);
}
But I may be completely off.
A better alternative is to declare the FileReference
as a member variable and add the EventHandler
in the constructor. You can remove all the EventHandlers
in the destructor.
It's also a good back-up to add a weak reference so that the listener is removed and garbage collected automatically when there are no references left pointing at it.
f.addEventListener(Event.COMPLETE,saveDone,false,0,true);
However, it's always best to make sure you manually remove a listener.
精彩评论