Actionscript 3 Actionscript:是否正确删除了该类实例?

Actionscript 3 Actionscript:是否正确删除了该类实例?,actionscript-3,Actionscript 3,好的,正确地删除,我的意思是,我实际上是在摆脱这个实例,还是它不再被绘制?我应该提到的是,我试图从实例自己的类中删除实例,也就是说,它会删除自身。它的工作原理是它绘制的正方形不再出现在屏幕上,但我不确定它是否真的消失了,或者只是没有被绘制。不管怎么说,这是一节课: package { import flash.display.*; import flash.events.*; public class OBJECT_bullet_1 extends Sprite

好的,正确地删除,我的意思是,我实际上是在摆脱这个实例,还是它不再被绘制?我应该提到的是,我试图从实例自己的类中删除实例,也就是说,它会删除自身。它的工作原理是它绘制的正方形不再出现在屏幕上,但我不确定它是否真的消失了,或者只是没有被绘制。不管怎么说,这是一节课:

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);
        }
    }
}
包
{
导入flash.display.*;
导入flash.events.*;
公共类对象\u bullet\u 1扩展了Sprite
{
公共变量X:int=0;公共变量Y:int=0;
公共变量Y_速度:int=5;
公共变量深度:int=9;
公共变量容器:Sprite=newsprite();
公共函数CREATE(容器:Sprite,X:int,Y:int):void
{
this.CONTAINER=容器;
CONTAINER.stage.addEventListener(事件输入帧,步骤);
这个.X=X;这个.Y=Y;
DRAW();
}
公共功能步骤(事件:事件):无效
{
这个.graphics.clear();
Y-=Y_速度;
如果(Y<20){Y=300;CONTAINER.removeChild(this);CONTAINER.stage.removeEventListener(Event.ENTER_FRAME,STEP);CONTAINER.(删除此);CONTAINER=null;return;}
DRAW();
}
公共函数DRAW():void
{
这个.graphics.beginll(0xCC00,1);
this.graphics.drawRect(X-2,Y-2,4,4);
this.graphics.endFill();
CONTAINER.addChild(this);
}
}
}

我关心的部分是阶跃函数,它检查Y是否小于20。你会注意到,它在课后做了几件事。我是否正确删除了它?如果是的话,我是否正在做一些不需要删除的事情?

两个问题都是。要确保删除对象,只需删除对该对象的所有引用。上面的代码只知道子引用和事件回调,您已经注意将它们都删除了。取消您自己的容器引用是不必要的,正如您认为的
容器一样。(删除此项)
所做的

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

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

}
//应该避免使用默认包。使用默认包
//如果您以后开始出现命名冲突,可能会造成困难。
包com.stackoverflow.example{
导入flash.display.Sprite;
导入flash.events.Event;
导入flash.geom.Point;
导入flash.utils.getTimer;
//类名是按惯例用CamelCase拼写的
//该“对象”在AS3中有特殊含义,因此您应该避免
//用它来指代其他任何东西。我在这里用了“实体”。
公共类EntityBullet1扩展了Sprite{
//使用时,ALLCAPS保留用于静态常量名称。
//静态常量的一个很好的用途是存储“幻数”。
公共静态常量默认颜色:uint=0xCC00;
公共静态常数默认值\u速度\u X:Number=0;
公共静态常量默认值\u速度\u Y:Number=-100;
公共静态常量默认大小:Number=4;
//我正在计算帧间的时间,以便运动更平稳。
公共变量上次:int;
公共变量颜色:uint=默认颜色;
公共变量大小:int=默认值大小;
//您可以使用Point类,而不是单独的x变量和y变量。
公共var位置:点;
公共var速度:点;
//不要使用“create”方法,而是在构造函数中进行所有创建!
公共函数EntityBullet1(x:Number=0,y:Number=0){
pos=新点(x,y);
速度=新点(默认速度X,默认速度Y);
//您不需要父容器来访问ENTER\u框架
//每个DisplayObject都有自己的。简单得多。
addEventListener(Event.ENTER_FRAME,第一步);
}
公共函数draw():void{
//将所有绘图保留在绘图功能内。以前,
//正在步骤方法内部调用clear()。
graphics.clear();
图形填充(彩色);
图形.drawRect(位置x-尺寸/2,位置y-尺寸/2,尺寸,尺寸);
graphics.endFill();
}
//在第一帧,字段“lastTime”仍然未初始化。
//此方法将其初始化为当前时间并停止
//将未来事件添加到适当的step()方法。
公共功能第一步(事件:事件):无效{
removeEventListener(Event.ENTER_FRAME,第一步);
addEventListener(Event.ENTER_帧,步骤);
lastTime=getTimer();
步骤(事件);
}
公共功能步骤(事件:事件):无效{
//要以固定速率移动,无论帧速率有多快,
//您需要计算时间增量。
var cur:int=getTimer();
变量增量:数字=(cur-lastTime)/1000.0;
lastTime=cur;
//位置等于速度乘以时间。
位置x+=速度x*增量;
位置y+=速度y*delta;
draw();
//请注意,所有DisplayObjects都已对其
//父容器称为“父”!
如果(位置y<20){
如果(parent!=null)parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME,步骤);
}
}
}
}

您已经远远超出了必要的范围;但是,你格式化if语句的方式让我想揍一顿。我的代码仍然有一点问题,但要正确地解决它,我必须解释弱引用是如何工作的。“我会让它保持原样的。”枪手47+1.回答得好。我不太明白