Flash AS3中的MovieClip移除

Flash AS3中的MovieClip移除,flash,actionscript-3,actionscript,Flash,Actionscript 3,Actionscript,所以昨晚我做了一个有趣的小项目,包括创建许多小圆圈(星星)。为了表示这颗星,我创建了一个星类。这里是最相关的方法 public class Star extends MovieClip { public function Star(r:Number) { starRadius = r; this.animated = false; this.graphics.beginFill(0xFFFFFF);

所以昨晚我做了一个有趣的小项目,包括创建许多小圆圈(星星)。为了表示这颗星,我创建了一个星类。这里是最相关的方法

public class Star extends MovieClip
{

public function Star(r:Number)
        {
            starRadius = r;
            this.animated = false;
            this.graphics.beginFill(0xFFFFFF);
            this.graphics.drawCircle(0,0,starRadius);
            this.graphics.endFill();

            this.addEventListener(Event.REMOVED, onRemoval);
        }
public function animate():void
        {
            if( isNull(angle)|| isNull(speed)){
                throw new Error("Angle or speed is NaN. Failed to animate");
            }
            if(animated){
                throw new Error("Star already animated");
            }
            if(this.parent == null){
                throw new Error("Star has not been added to stage yet");
            }

            this.addEventListener(Event.ENTER_FRAME, doAnimate, false, 0);
            animated = true;
        }

private function doAnimate(e:Event):void{

            if(isNull(cachedDirectionalSpeedX) || isNull(cachedDirectionalSpeedY)){
                cachedDirectionalSpeedY = -speed * Math.sin(angle); 
                cachedDirectionalSpeedX = speed * Math.cos(angle);
            }

            this.x += cachedDirectionalSpeedX;
            this.y += cachedDirectionalSpeedY;


            if(this.x > this.parent.stage.stageWidth || this.y > this.parent.stage.stageHeight){
                this.dispatchEvent(new Event("RemoveStar",true));
                this.removeEventListener(Event.ENTER_FRAME, doAnimate);

            }


        }
为了给您一个关于它所做工作的总结,基本上在初始化时,它向自身添加了一个监听器,它基本上只是为每个实例变量设置空值。调用animate()时,它会向自身添加一个监听器,该监听器会向特定方向设置动画。此侦听器还检查其位置是否已在舞台之外,并在舞台之外时停止其移动。此外,它将调度一个事件,以便父级知道何时删除它

所以在“主要”课上,我有

this.addEventListener("RemoveStar", removeStar);


我有另一个监听器,它基本上每次都打印出孩子的数量,但我不会把代码放在这里。我的问题是。。。看起来,删除星星的侦听器“并非一直”工作。当在初创期创造1000个明星时,其他什么都没有,孩子的数量在一开始就下降了,并且停留在某个特定的数字上,这让我觉得有些电影剪辑没有被删除。有人知道这里发生了什么吗?

检查您是否正在删除所有事件侦听器,例如removeStar。。。我不知道如何将removeStar侦听器附加到“Main”上,但removeStar函数正确地引用了e.target属性作为星形

基本上,您需要“杀死”所有对对象的引用,以便在将来GC执行其传递时“释放”对象


虽然您没有“删除”所有星星,但原因可能是您的“删除检查”代码中只检查了右边缘和下边缘。。。可能有些星星向左或向上。。。为left和up添加附加条件,它可能会自行修复。。。(this.x<0 | | this.y<0)

您能给我们看一下完整的代码吗?缺少了不同的东西:所有的类属性和一些类方法,如isNull()或onremove(),其中的
onremove()
函数应该删除所有的内部引用吗?不过还是要感谢这个附加条件。我忘了
private function removeStar(e:Event):void
        {
            starCount--;
            this.removeChild(Star(e.target));
        }