即使 uiloader 已卸载,EventListener 仍处于活动状态
我目前在父影片剪辑中使用三个 uiloader,它加载其他三个 swf 文件。每个影片剪辑都有键盘事件,敲击时会发出不同的声音。
当我转到影片剪辑并返回父级时遇到问题,事件侦听器仍然处于活动状态。我一直在尝试不同的操作,例如从 uiloader 中卸载 swf 文件,但该事件仍然处于活动状态,即使在我失去焦点后也可以直接访问它。
我很确定问题是我在舞台上有事件侦听器,但在退出 swf 文件后我真的不知道如何卸载它。
任何帮助将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你是对的。键盘事件通常在舞台上注册(即 stage.addEventListener( KeyboardEvent.KEY_UP, someFunction ); ),这意味着舞台保存对影片剪辑的引用,防止它被垃圾收集,即使您尝试卸载它。
有两种方法可以解决这个问题。您要么必须取消注册 KeyboardListener stage.removeEventListener( KeyboardEvent.KEY_UP, someFunction );或者您可以将侦听器注册为弱引用:
stage.addEventListener( KeyboardEvent.KEY_UP, someFunction, false, 0, true );
其中最后一个参数 (true) 表示该事件被注册为弱引用。默认为 false。
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.