2010-08-17 5 views
2

좋아요 올바르게 삭제하면 실제로 인스턴스를 제거하고 있거나 더 이상 그려지지 않고 있습니다. 나는 자체 클래스 내에서 인스턴스를 삭제하려고한다는 것을 언급해야한다. 즉, 자신을 삭제하는 것이다. 더 이상 화면에 그려지지 않는 광장에서 작동하지만 실제로 사라 졌는지 또는 그려지지 않았는지 확실하지 않습니다. 어쨌든 여기에 클래스가있다 :액션 스크립트 :이 클래스 인스턴스를 올바르게 삭제합니까?

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

그것은 당신이 afterwords 여러 가지 작업을 수행 것을 알 수 있습니다 Y < 20 있는지 확인합니다 때 STEP 기능에 대해 내가 우려하는 부분입니다. 올바르게 삭제합니까? 그렇다면 삭제할 작업이 필요하지 않습니다.

+4

당신은 필요한 것을 훨씬 뛰어 넘었습니다. 하지만, 당신이 if 문을 형식화하는 방식은 내가 아기를 때려주고 싶어합니다. – Aaron

답변

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

+0

내 코드에는 여전히 약간의 문제가 있지만 적절하게 해결하려면 약한 참조가 작동하는 방식을 설명해야합니다. 나는 그것을 그대로 두겠습니다. – Gunslinger47

+1

@ 건슬링거 47. +1. 좋은 대답. 그래도 나는 작은 문제를 보지 못한다. –