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 rea开发者_如何学JAVAlly know how to unload it once I'm out of the swf file.
Any help will be greatly appreciated.
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.
精彩评论