删除 children 后后台进程继续

Background processes continue after removing children

我制作了这个在我程序的 start-up 处启动的菜单。 (Main.as:启动 mainMenu.asmainMenu.as:启动自身内部的其他实例。)这个 mainMenu 顶部有一个按钮,上面写着 "new game",我想要这个按钮带我到下一个菜单,从而删除 mainMenu-实例。

我确实成功删除了 mainMenu。然而,我通过在我的一个 class 中使用 trace() 发现一个函数仍然在后台 运行。 (这是 mainMenu-实例中实例的 class。

我已尝试使用以下方法消除 mainMenu-实例:

this.parent.removeChild(this);
trace("all processes shall now be over");

并且,通过使用以下方法删除 mainMenu 中的所有 children:

while (this.numChildren > 0) {
    this.removeChildAt(0);
}
trace("there shall be nothing going on after this");

这已经从屏幕上删除了所有视觉内容。但是来自 mainMenu 中的一个实例的 timer-loop-function 仍然在后台 运行,再次被 trace() 辱骂。这些children到这里应该都被淘汰了,怎么这些进程还是运行?这个菜鸟需要帮助。

从显示列表中删除对象不会立即强制对其进行垃圾回收。您定义的任何计时器将一直触发,直到它们被停止或被垃圾回收。

您可以通过搜索 "flex memory management" 或 "actionscript 3 memory management" 来阅读有关此主题的更多信息。

关于您的具体菜单示例,更新您的代码如下:

你的菜单 class 和菜单的子菜单 class 都应该实现一个接口 "ITimerUser",它指定了一个 "stopTimers" 函数 - 它看起来像这样:

public function stopTimers():void {
    //If there is a timer defined
    this.myTimer.stop();
    this.myTimer = null;
}

然后将您的删除代码更改为如下所示(n.b。如果您混入了非计时器用户,请将 stopTimers 调用设为可选):

while (this.numChildren > 0) {
    var child = (ITimerUser) this.getChildAt(0);
    if (child) {
        child.stopTimers();
    }
    this.removeChildAt(0);
}
this.stopTimers();
this.parent.removeChild(this);