2011-07-20 168 views
-1

所以我创造了一个有趣的小项目昨晚涉及创建大量的小圆圈(星)。所以为了代表这颗明星,我创建了一个明星班。这是最相关的方法影片剪辑去除AS3

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); 

      } 


     } 

为了给你什么它做了总结,基本上就初始化它增加了对自身的监听器,基本上是空值每个实例varialbles。当animate()被调用时,它会向本身添加一个侦听器,并将其动画到某个方向。这个听众还检查它的位置是否已经在舞台之外,并且在它已经到达时停止它的移动。另外,它会派发一个事件,以便父母知道何时删除它。

因此,在“主”类,我有

this.addEventListener("RemoveStar", removeStar); 

private function removeStar(e:Event):void 
     { 
      starCount--; 
      this.removeChild(Star(e.target)); 
     } 

我还有一个听众,基本上打印出数儿,它有每次,但我不是要去把代码放在这里。我遇到的问题是......看起来像删除星星的听众不能“一直”工作。当创建1000个明星在启动时,没有别的,儿童人数下降在开始的时候,它被卡在一定数量这使我觉得有一些影片剪辑不会移除。有人知道这里发生了什么吗?

+1

你能向我们展示完整的代码?有不同的东西丢失:所有的类属性,以及一些类的方法,如ISNULL()或onRemoval() – pkyeck

+1

哪里'onRemoval()'函数,想必应该去掉所有的内部引用? – shanethehat

回答

0

检查您是否正在移除所有事件侦听器,例如removeStar ...我没有看到如何将removeStar侦听器附加到“Main”,但remove star函数正确引用e.target属性作为星号...

基本上,你需要为它被“释放”在将来某个时候,当GC做它的传球“杀死”的对象的所有引用。

原因虽然你不是“删除”所有的星星,大概只有右侧和底部边缘都在你的“删除检查”代码进行核对......可能有些明星去左或向上......添加更多的条件进行左,并可能自我修复...(this.x < 0 || this.y < 0)

+0

感谢您的额外条件。总结忘了 – denniss