为什么我的事件侦听器触发不止一次? [英] Why are my event listeners firing more than once?
问题描述
在我的Flash项目中,我有一个带有2个关键帧的动画片段.这两个帧均包含1个动画片段.
In my Flash project I have a movieclip that has 2 keyframes. Both frames contain 1 movieclip each.
帧1-Landing
框架2-Game
frame 1 - Landing
frame 2 - Game
应用程序的流程很简单:
The flow of the application is simple:
- 用户到达目标网页(第1帧)
- 用户单击开始游戏"按钮
- 将用户带到游戏页面(第2帧)
- 游戏结束后,用户可以按再次玩"按钮,将他们带回到步骤1
Landing
和Game
电影剪辑都链接到定义事件侦听器的单独类.问题是,当我玩完游戏后回到步骤1时,Game
事件侦听器会针对各自的事件触发两次.如果我第三次执行该过程,则事件侦听器将为每个事件触发三遍.这一直在发生,因此,如果我遍历应用程序流7次,则事件侦听器将触发7次.我不明白为什么会这样,因为在第1帧上,Game
movieclip(我会假定其相关的类实例)不存在-但我在这里显然缺少了一些东西.
Both Landing
and Game
movieclips are linked to separate classes that define event listeners. The problem is that when I end up back at step 1 after playing the game, the Game
event listeners fire twice for their respective event. And if I go through the process a third time, the event listeners fire three times for every event. This keeps happening, so if I loop through the application flow 7 times, the event listeners fire seven times. I don't understand why this is happening because on frame 1, the Game
movieclip (and I would assume its related class instance) does not exist - but I'm clearly missing something here.
我也在其他项目中也遇到了这个问题,并尝试通过首先检查事件侦听器是否存在并仅在不存在事件侦听器的情况下进行定义来解决此问题,但最终结果出人意料,但并未真正解决问题.
I've run into this problem in other projects too, and tried fixing it by first checking if the event listeners existed and only defining them if they didn't, but I ended up with unexpected results that didn't really solve the problem.
我需要确保事件侦听器仅触发一次.任何建议和见识将不胜感激,谢谢!
I need to ensure that the event listeners only fire once. Any advice & insight would be greatly appreciated, thanks!
推荐答案
如果在同一层上有两个带有不同剪辑的帧,则每当该帧进入"时,都会创建该剪辑.当离开"剪辑时,将其删除,但仍会保留在该剪辑中,并且不会被垃圾回收.因此,下一次该帧进入另一个剪辑时,将创建"它并获得自己的侦听器.最好的选择是在更改帧时删除侦听器.我通常在每个类中都有一个Event.REMOVED_FROM_STAGE
侦听器来解决这种问题.如果将其删除,那么您将清理其余的侦听器.
If you have two frames with different clips on the same layer, each time that frame "enters", that clip is created. When it "leaves" the clip is removed, but it still will be kept around and not be garbage collected. So the next time the frame enters a different clip is "created" and it gets its own listener. Your best bet is to remove the listeners when you change frames. I usually get around this kind of stuff by having a listener for Event.REMOVED_FROM_STAGE
in each class. If it's removed, then you clean up the remaining listeners.
您可能还想尝试弱听者":
You may also want to experiment with "weak listeners":
addEventListener(GameEvent.GAME_START, gameStartedHandler, false, 0, true);
"true"使链接弱",因此,如果删除了对象,则垃圾收集器可以将其拾取.我不会完全依靠这个.最好手动删除引用,这样就可以确定.
The "true" makes the link "weak" so if an object is removed, the garbage collector can pick it up. I wouldn't rely on this completely though. Better to manually remove references so you can be sure.
这个答案有很多道理.如果您张贴代码/屏幕截图/flas来更好地进行诊断,将会很有帮助.
This answer assumes a lot of course. It'd be helpful if you posted code/screenshots/flas to better diagnose.
这篇关于为什么我的事件侦听器触发不止一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!