为什么不删除事件侦听器的工作? [英] Why doesn't removing event listeners work?
问题描述
我有这个在我的构造函数:
I have this in my constructor:
addEventListener(Event.REMOVED_FROM_STAGE, actualDestroy);
和这actualDestroy:
And this in actualDestroy:
public function actualDestroy(e:* = null){
removeEventListener(Event.REMOVED_FROM_STAGE,actualDestroy);
if(this.parent){
this.parent.removeChild(this);
}
}
现在的问题是我得到的错误:错误#2094:事件调度递归溢出。为什么removeChild之不断得到所谓的,如果this.parent不存在?为什么不删除事件侦听器的工作?
The problem is I get Error: Error #2094: Event dispatch recursion overflow. Why does removechild keep getting called if this.parent does not exist? Why doesn't removing event listeners work?
推荐答案
本次活动的名称是一种误导。 removedFromStage
,根据文档,是时调度显示对象是关于从显示列表中删除。换句话说,这是发生了什么事在code:
The name of the event is misleading. removedFromStage
, according to the docs, is "dispatched when a display object is about to be removed from the display list". In other words, this is what's happening in your code:
- 放在你的code,你叫
parent.removeChild(本)
-
actualDestroy
立即调用。在这一点上,<STRONG>的对象仍然显示列表中,所以this.parent!= NULL
- 在
actualDestroy
,你叫parent.removeChild(本)
试。 - 转到步骤2
- Somewhere in your code, you call
parent.removeChild(this)
actualDestroy
is immediately called. At this point, the object is still in the display list, sothis.parent != null
- In
actualDestroy
, you callparent.removeChild(this)
again. - Go to step 2
因此,要解决这个问题,你可能需要重构你的code(一个对象从显示列表中删除本身是不是一个好主意无论如何),或者使用一些布尔像 beingRemoved
来检查对象是否被从列表中删除了。在这种情况下,不叫 parent.removeChild(本)
在 actualDestroy
。
So to fix the issue, you might want to refactor your code (an object removing itself from the display list is never a good idea anyway), or perhaps use some boolean like beingRemoved
to check whether the object is being removed from the list already. In which case, don't call parent.removeChild(this)
in actualDestroy
.
这篇关于为什么不删除事件侦听器的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!