0

I'm currently using three uiloaders inside a parent movieclip which loads three other swf files. Each of these movieclips have keyboard events that when struck call different sounds.

I'm having a problem when I go to a movieclip and return to the parent, the event listener is still active. I've been trying different things like unloading the swf file from the uiloader, but the event is still active and can access is it directly even after I'm out of focus.

I'm pretty sure the problem is that I have the event listener on the stage, but don't really know how to unload it once I'm out of the swf file.

Any help will be greatly appreciated.

triangulito
  • 202
  • 2
  • 16

1 Answers1

1

You are correct. Keyboard events are most often registered with the stage (i.e. stage.addEventListener( KeyboardEvent.KEY_UP, someFunction ); ), which means that the stage holds a reference to your movieclip preventing it from beeing garbage collected, even if you try to unload it.

There are two ways you can get around this. You either have to unregister the keyboardListener stage.removeEventListener( KeyboardEvent.KEY_UP, someFunction ); or you can register the listener as a weak reference:

stage.addEventListener( KeyboardEvent.KEY_UP, someFunction, false, 0, true );

where the last argument (true) means that the event is registred as weak reference. Defaults to false.

Tommislav
  • 66
  • 3
  • How exactly does the weak reference work, do I have to unload the swf file for it to actually be collected or can I just lose focus of it? It seems like this is exactly what I need, but don't really understand how to manage it. – triangulito May 05 '11 at 16:20
  • If you use weak references then you should not be able to get any more key events after you have removed the swf. But the most correct thing to do is to would be to call removeEventListeners just before you unload the swf! Maybe put a destroy()-method in your loaded swf that cleans itself up. I would recommend doing that. – Tommislav May 06 '11 at 09:46