Flash AS3 如何删除舞台事件侦听器 [英] flash AS3 how do I remove stage event listeners

查看:42
本文介绍了Flash AS3 如何删除舞台事件侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 flash 和 as3 制作动画,我有一个添加舞台的功能eventListener, stage.addEventListener(Event.ENTER_FRAME, setScrollPercent, false, 0, true);

I'm building an animation in flash and as3, I have a function in which I add a stage eventListener, stage.addEventListener(Event.ENTER_FRAME, setScrollPercent, false, 0, true);

由于此事件是在一个函数的函数内部设置的,两个函数深",如何从函数外部重置所有阶段事件侦听器而不会出错?

Since this event is set inside a function of a function, "two functions deep," How can I reset all stage event listeners from outside of the functions without getting an error?

推荐答案

AS 事件监听器的最佳实践是:

Best practices with AS eventlisteners are:

  • 让它变弱(就像你所做的那样,addEventListener 的最后一个参数)
  • 处理后将事件监听器设置为null(严格来说如果设置为弱则不需要)

Flex 不提供析构函数.它有自己的垃圾收集器在管理内存的幕后运行.一旦没有对它的引用,它就会清理一块内存.嗯,强引用.默认情况下,所有对象都具有强引用.事件处理程序,因为它们往往是性能瓶颈,所以有这种声明自己弱的特殊能力——弱引用.这当然只有在您使用 addEventHandler() 函数附加事件处理程序时才有可能.弱引用不会被 GC 考虑在内,因此,当所有强引用都消失时,它们将被自动垃圾收集,让您摆脱必须手动执行 =null 的压力.当您不指定参数时,这就是您要执行的操作.默认情况下,偶数处理程序被创建为强引用.

Flex does not give you destructors. It has its own Garbage Collector running behind the scenes managing memory. It cleans up a piece of memory once there are no references to it. Well, strong references. All objects by default have a strong reference. Event handlers, since they tend to be a performance bottleneck, have this special ability of declaring themselves weak -- a weak reference. This is of course possible only when you are attaching the event handlers using the addEventHandler() function. Weak references are not taken into account by the GC and hence, when all strong references are gone, they'll be automagically garbage collected, freeing you from the tension of having to do a =null manually. Which is what you'd otherwise do when you don't specify the parameter. By default, even handlers are created as strong references.

但是,将它们标记为弱会产生副作用.他们可以在你不知道的情况下消失在空气中.最终,你会知道,但在一个讨厌的错误方面.那是导致你问题的原因吗?也许,也许不是.你必须试验.此外,如果您能向我们提供更多详细信息(例如确切的错误代码、某些来源),也会有所帮助.

However, marking them weak has a side-effect. They can vanish into thin air without you ever knowing about it. Eventually, you will know, but in terms of a nasty bug. Is that what's causing your problems? Maybe, may be not. You'll have to experiment. Also, it'll help if you can provide us with some more detail like the exact error code, some source.

希望这会有所帮助.快乐弯曲:)

Hope this helps. Happy flexing :)

这篇关于Flash AS3 如何删除舞台事件侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆