简体   繁体   English

flash AS3 如何删除舞台事件监听器

[英]flash AS3 how do I remove stage event listeners

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);我正在 flash 和 as3 中构建一个 animation,我有一个 function 在其中我添加了一个舞台 eventListener,stage.addEventListener(Event.0ENTER);

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?由于此事件是在 function 的 function 中设置的,“两个函数深度”,如何从函数外部重置所有阶段事件侦听器而不会出现错误?

Best practices with AS eventlisteners are: AS 事件监听器的最佳实践是:

  • Make it weak (as you've done, the last parameter of addEventListener)让它变弱(就像你所做的那样,addEventListener的最后一个参数)
  • Set the event listener to null after handling (strictly speaking not required if you have set it to be weak)处理后将事件监听设置为null(严格来说如果设置为weak则不需要)

Flex does not give you destructors. Flex 没有给你析构函数。 It has its own Garbage Collector running behind the scenes managing memory.它有自己的垃圾收集器在后台运行,管理 memory。 It cleans up a piece of memory once there are no references to it.一旦没有对它的引用,它就会清理一块 memory。 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.这当然只有在您使用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. GC 不考虑弱引用,因此,当所有强引用都消失时,它们将自动被垃圾收集,从而使您摆脱必须手动执行=null的压力。 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:)快乐弯曲:)

What errors are you seeing?你看到了什么错误? The nesting level shouldn't have anything to do with it, since the listeners are just registered by the parameters, so as long you call remove with the same three key parameters you used for add, you should be fine.嵌套级别应该与它无关,因为侦听器只是通过参数注册的,所以只要您使用与添加相同的三个关键参数调用 remove,就可以了。

Is your real question how to get a reference to the listener object to the outside scope?您真正的问题是如何获得对外部 scope 的监听器 object 的引用? If so, there are several possible solutions and the best way to do it depends on the structure of your code.如果是这样,有几种可能的解决方案,最好的方法取决于代码的结构。

-- MarkusQ ——马库斯

You should be able to just use your old function and use removeEventListeners instead of add.您应该可以只使用旧的 function 并使用 removeEventListeners 而不是 add。 Quite simple actually.其实很简单。

The answer to your question, and I realize you had a problem with scope, because I just answered a question you had on scope, is that you are working with stage.您的问题的答案,我知道您对 scope 有疑问,因为我刚刚回答了您对 scope 提出的问题,即您正在使用舞台。 Consider the stage global as it is the canvas in which all of your display objects are drawn.考虑全局舞台,因为它是绘制所有显示对象的 canvas。

The following will work anywhere!!!以下将在任何地方工作!!!

stage.addEventListener(Event.ENTER_FRAME, setScrollPercent, false, 0, true);

Now, the error that your talking about, IM GUESSING, is that you merely set the above to removeEventListener when you were ready which will not work.现在,您所说的错误,IM GUESSING,是您仅在准备好时将上述内容设置为 removeEventListener ,这是行不通的。

The removeEventListener function DOES NOT accept five parameters like its sibling addEventListener, it only allows three ignoring priority and weak-reference, as the following shows. removeEventListener function 不接受与其兄弟 addEventListener 一样的五个参数,它只允许三个忽略优先级和弱引用,如下所示。

//The following removes YOUR stage Event.ENTER_FRAME listener from any scope.
stage.removeEventListener(Event.ENTER_FRAME, setScrollPercent, false);

hope this helps, remember that stage is the global root in a sense, and be careful of root, it actually works how it is supposed to now in the fact that calling root is now relative to the swf you call it from, not the stage, unless the stage is the root of the current scope.希望这会有所帮助,请记住,从某种意义上说,stage 是全局根,并且要小心 root,它实际上是按现在应该的方式工作的,因为调用 root 现在是相对于你调用它的 swf,而不是 stage , 除非阶段是当前 scope 的根。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM