闪光AS3如何删除阶段事件监听器监听器、闪光、阶段、事件

2023-09-08 12:45:35 作者:催泪罪供

我要建在闪存和AS3动画,我有我在其中添加一个阶段的函数 事件监听,stage.addEventListener(Event.ENTER_FRAME,setScrollPercent,假,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 EventListeners的是最佳做法:

Best practices with AS eventlisteners are:

请其弱(因为你所做的,的addEventListener的最后一个参数) 设置事件侦听器处理(严格来说不是必需的,如果你把它设置为弱)后空

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.

然而,将它们标记弱具有副作用。他们可以成为泡影没有你永远知道这件事。最终,你会知道,但在一个讨厌的错误方面。那是什么导致你的问题?也许,可能不会。你必须尝试。此外,它会帮助,如果你能为我们提供像确切的错误code,部分源一些细节。

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 :)