问题描述
开展以下code在ADL的时候,为什么方继续转动?
when launching the following code in ADL, why does the square continue to rotate?
var square:Sprite = new Sprite();
square.graphics.beginFill(0xFF0000);
square.graphics.drawRect(-25, -25, 50, 50);
square.x = square.y = 100;
addChild(square);
addEventListener(Event.ENTER_FRAME, rotateSquare, false, 0, true);
function rotateSquare(evt:Event):void
{
square.rotation += 2;
}
System.gc();
更新
下面的显示对象具有弱引用ENTER_FRAME事件侦听器。然而,美其名曰:
Update
the following display object has a weak referenced ENTER_FRAME event listener. however, calling:
removeChild(testInstance);
testInstance = null;
不停止ENTER_FRAME事件:
doesn't stop the ENTER_FRAME event:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Test extends Sprite
{
private var square:Sprite;
public function Test()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(evt:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
square = new Sprite();
square.graphics.beginFill(0xFF0000);
square.graphics.drawRect(-25, -25, 50, 50);
square.x = square.y = 100;
addChild(square);
addEventListener(Event.ENTER_FRAME, rotateSquare, false, 0, true);
// //Current Solution - only works on display objects
// addEventListener(Event.REMOVED_FROM_STAGE, removeHandler);
}
private function rotateSquare(evt:Event):void
{
trace("square is rotating");
square.rotation += 2;
}
// private function removeHandler(evt:Event):void
// {
// removeEventListener(Event.REMOVED_FROM_STAGE, removeHandler);
// removeEventListener(Event.ENTER_FRAME, rotateSquare);
// }
}
}
我添加了一个REMOVED_FROM_STAGE事件侦听器,但这只能在显示对象。
i have added a REMOVED_FROM_STAGE event listener, but this will only work on display objects.
是特定于ENTER_FRAME事件?
is this problem specific to ENTER_FRAME event?
推荐答案
Flash的垃圾回收仅清除指出,要么是零引用计数或只有弱引用元素/对象/变量。
Flash's garbage collection only clears out elements/objects/variables that have either a zero reference count or have only weak references.
这意味着你需要做到这一点。为了使它真正得到gc'd。
This means you would need to do this. For it to truly be gc'd.
removeChild(square)
square = null;
System.gc()
这篇关于动作 - 强制垃圾收集不工作在日常生活?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!