2010-08-17 37 views
2

好吧,通过正确删除我的意思是我实际上摆脱了实例,还是只是没有被绘制了?我应该提到,我试图从自己的类中删除实例,也就是删除它自己。它“起作用”,因为它所画的正方形不再出现在屏幕上,但我不确定它是否真的已经消失或者没有被绘制。反正这里的类:Actionscript:我是否正确删除这个类实例?

package 
{ 
    import flash.display.*; 
    import flash.events.*; 
    public class OBJECT_bullet_1 extends Sprite 
    { 
     public var X:int = 0; public var Y:int = 0; 
     public var Y_SPEED:int = 5; 
     public var DEPTH:int = 9; 
     public var CONTAINER:Sprite = new Sprite(); 
     public function CREATE(CONTAINER:Sprite,X:int,Y:int):void 
     { 
      this.CONTAINER = CONTAINER; 
      CONTAINER.stage.addEventListener(Event.ENTER_FRAME,STEP); 
      this.X = X;  this.Y = Y; 
      DRAW(); 
     } 
     public function STEP(event:Event):void 
     { 
      this.graphics.clear(); 
      Y -= Y_SPEED; 
      if (Y < 20) {Y = 300; CONTAINER.removeChild(this); CONTAINER.stage.removeEventListener(Event.ENTER_FRAME,STEP); CONTAINER.(delete this); CONTAINER = null; return;} 
      DRAW(); 
     } 
     public function DRAW():void 
     { 
      this.graphics.beginFill(0xCCCC00,1); 
      this.graphics.drawRect(X - 2,Y - 2,4,4); 
      this.graphics.endFill(); 
      CONTAINER.addChild(this); 
     } 
    } 
} 

我关心的是在STEP功能时,它会检查如果Y < 20.你会发现,它确实几件事情后记部分。我是否正确删除它?如果有,我正在做什么来删除它,我不需要?

+4

你超出了必要的范围;但是,你对if语句进行格式化的方式让我想要打个小宝宝。 – Aaron 2010-08-17 03:13:21

回答

4

对两个问题都可以。为确保删除对象,您只需删除对它的所有引用。子代码和事件回调是上述代码唯一知道的,并且您已经注意将它们都删除。废除自己的容器参考是不必要的,因为无论你认为CONTAINER.(delete this)是什么。

您提供的代码还有一些其他重大问题。我做了一些改进,并对所有更改进行了严格评论,以解释我为什么做出这些改动

// You should avoid using the default package. Using the default package 
// can make it difficult later on if you start having naming conflicts. 
package com.stackoverflow.example { 

    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.geom.Point; 
    import flash.utils.getTimer; 

    // Class names are spelled in CamelCase by convention. Also, note 
    // that "Object" has a special meaning in AS3 so you should avoid 
    // using it to refer to anything else. I used here "Entity" instead. 
    public class EntityBullet1 extends Sprite { 
     // ALLCAPS when used are reserved for static const names. 
     // A good use of static consts is to store "magic numbers". 
     public static const DEFAULT_COLOR:uint  = 0xCCCC00; 
     public static const DEFAULT_SPEED_X:Number = 0; 
     public static const DEFAULT_SPEED_Y:Number = -100; 
     public static const DEFAULT_SIZE:Number = 4; 

     // I'm calculating the time between frames for smoother movement. 
     public var lastTime:int; 
     public var color:uint = DEFAULT_COLOR; 
     public var size:int = DEFAULT_SIZE; 

     // Instead of separate x and y vars, you can use the Point class. 
     public var pos:Point; 
     public var speed:Point; 

     // Instead of a "create" method do all creation inside the constructor! 
     public function EntityBullet1(x:Number = 0, y:Number = 0) { 
      pos = new Point(x, y); 
      speed = new Point(DEFAULT_SPEED_X, DEFAULT_SPEED_Y); 

      // You don't need the parent container to access the ENTER_FRAME 
      // event. Every DisplayObject has its own. Much simpler. 
      addEventListener(Event.ENTER_FRAME, firstStep); 
     } 

     public function draw():void { 
      // Keep all drawing inside the draw function. Previously, 
      // clear() was being called inside the step method. 
      graphics.clear(); 
      graphics.beginFill(color); 
      graphics.drawRect(pos.x - size/2, pos.y - size/2, size, size); 
      graphics.endFill(); 
     } 

     // On the first frame, the field "lastTime" is still uninitialized. 
     // This method initializes it to the current time and hands off 
     // future events to the proper step() method. 
     public function firstStep(event:Event):void { 
      removeEventListener(Event.ENTER_FRAME, firstStep); 
      addEventListener(Event.ENTER_FRAME, step); 
      lastTime = getTimer(); 
      step(event); 
     } 

     public function step(event:Event):void { 
      // To move at a fixed rate regardless of how fast the framerate is, 
      // you need to calculate the time delta. 
      var cur:int = getTimer(); 
      var delta:Number = (cur - lastTime)/1000.0; 
      lastTime = cur; 

      // Position equals velocity times time. 
      pos.x += speed.x * delta; 
      pos.y += speed.y * delta; 

      draw(); 

      // Note that all DisplayObjects already have references to their 
      // parent containers called "parent"! 
      if (pos.y < 20) { 
       if (parent != null) parent.removeChild(this); 
       removeEventListener(Event.ENTER_FRAME, step); 
      } 
     } 
    } 

} 
+0

超出和超出,+1 – Aaron 2010-08-17 03:15:45

+0

我的代码仍然有一个小问题,但为了正确解决它,我必须解释弱引用是如何工作的。我会保持原样。 – Gunslinger47 2010-08-17 05:33:21

+1

@ Gunslinger47。 +1。很好的答案。不过,我不太明白这个小问题。 – 2010-08-17 14:11:41