Actionscript 3 我收到一个错误#1009,表示我无法访问空对象引用的属性或方法。现在怎么办?

Actionscript 3 我收到一个错误#1009,表示我无法访问空对象引用的属性或方法。现在怎么办?,actionscript-3,flash,actionscript,flash-cs6,Actionscript 3,Flash,Actionscript,Flash Cs6,所以我在尝试运行游戏时出错了。这是一个简单的小游戏,围绕着拾起轨道上的杰瑞罐头,同时试图避免轨道上的敌人。所以我点击Ctrl+Shft+Enter,发现问题出在我的Ship类的第26行(如果是(this.y+…) package { import flash.display.Sprite; import flash.events.Event; public class Ship extends Sprite { public functi

所以我在尝试运行游戏时出错了。这是一个简单的小游戏,围绕着拾起轨道上的杰瑞罐头,同时试图避免轨道上的敌人。所以我点击Ctrl+Shft+Enter,发现问题出在我的Ship类的第26行(如果是(this.y+…)

package 
{

    import flash.display.Sprite;
    import flash.events.Event;


    public class Ship extends Sprite
    {

        public function Ship(_x:int,_y:int)
        {
        this.x = _x;
        this.y = _y;
        //adds event listener that allows the player to move
        addEventListener(Event.ENTER_FRAME, player_move);
    }

    public function player_move(e:Event)
    {
        //check if at left or right side of stage
        if (this.y - this.height / 2 <= 0)
        {
            this.y = 50;
        }
        if (this.y + this.height / 2 >= stage.height - this.height)
        {
            this.y = 370;
        }
        if (this.x - this.width / 2 <= 0)
        {
            this.x = 50;
        }
        if (this.x + this.width / 2 >= stage.width - this.width)
        {
            this.x = 500;
        }


    }

    public function left():void
    {
        //the speed in which the player will move left
        this.x -=  10;
    }
    public function right():void
    {
        //the speed in which the player will move right
        this.x +=  10;
    }
    public function up():void
    {
        //the speed in which the player will move right
        this.y -=  10;
    }
    public function down():void
    {
        //the speed in which the player will move right
        this.y +=  10;
    }
  }
}
包
{
导入flash.display.Sprite;
导入flash.events.Event;
公共级船舶扩展Sprite
{
公共功能船(x:int,y:int)
{
this.x=x;
this.y=\u y;
//添加允许玩家移动的事件侦听器
addEventListener(事件。输入帧,玩家移动);
}
公共功能玩家移动(e:事件)
{
//检查是否在舞台左侧或右侧
if(this.y-this.height/2=stage.height-this.height)
{
这个y=370;
}
if(this.x-this.width/2=stage.width-this.width)
{
这个x=500;
}
}
公共函数left():void
{
//玩家向左移动的速度
这个.x-=10;
}
公共功能权限():无效
{
//玩家向右移动的速度
这个.x+=10;
}
公共函数up():void
{
//玩家向右移动的速度
这个.y-=10;
}
公共函数down():void
{
//玩家向右移动的速度
这个.y+=10;
}
}
}
现在我该怎么办?我该怎么解决这个问题?我到处都找不到答案。我知道这与我的主要职业有关,因为在这本书中,我已经说过,如果玩家的对手是敌人,他的船将被放回原来的位置


任何帮助都将不胜感激。谢谢。

您的空对象是
阶段
引用。每个DisplayObject都有一个对阶段的引用,但是,在对象实际位于阶段之前,该对象是空的

阶段是应用程序的主容器。应用程序中的所有可视内容都将以某种方式显示在阶段上。您的主文档类将显示在阶段上,所有时间线对象等

即使将对象添加到另一个容器中,只要该容器以某种方式在舞台上,您的对象也会被视为在舞台上。因此,用最基本的术语来说,如果对象位于用户应该能够看到它的某个地方,舞台将不会为空

要解决此问题,您必须在将对象添加到后台后添加
ENTER\u FRAME
事件侦听器。幸运的是,您可以侦听在发生此情况时触发的事件

在构造函数中:

addEventListener(Event.ADDED_TO_STAGE, init);
然后添加处理程序:

private function init(evt:Event){
    addEventListener(Event.ENTER_FRAME, player_move);
}

请记住,
stage
将为空,直到将对象添加到stage,这是我们现在正在侦听的事件。然后,只需将您的飞船添加到主游戏或它将进入的任何容器中,
container.addChild(ship)
,如果该容器是阶段的一部分,您应该很乐意去做。

它被添加到阶段了吗?
stage
可能是一个无效的引用。很抱歉我不知道,我对这一切都是新手。但是,您的确切意思是它被添加到阶段了吗?我到底需要做什么才能将它添加到阶段或制作
stage
有效的引用?player\u mve方法的第一行应该是:if(!stage){return;}