-1

我正在创建一个射击游戏。当我决定创建一种在到达舞台顶部后移除导弹的方法时,我的代码遇到了问题。我可以毫无问题地运行程序,只有我意识到如果我按住射击按钮,导弹并没有从舞台上移开。但是,如果我点击射击按钮,导弹将被移除,并在输出中打印出此错误 #1009。

有什么办法可以解决这个问题吗?

这是导弹飞到舞台顶部并启用调试后的错误:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Missle/destroyMissle()[E:\Experiment\ExperimentProject\Missle.as:39]
at main/checkMissleOffScreen()[E:\Experiment\ExperimentProject\main.as:63]
at main/eventUpdated()[E:\Experiment\ExperimentProject\main.as:51]

这是主要的课程:

package  
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;

/**
 * ...
 * @author test
 */
public class main extends MovieClip
{
    //Objects
    public var rect:MovieClip;
    public var missle:Missle;

    //Array
    private var missleArray:Array;

    //Keyboard section
    var leftKeyIsDown:Boolean;
    var rightKeyIsDown:Boolean;
    var upKeyIsDown:Boolean;
    var downKeyIsDown:Boolean;
    var spaceKeyIsDown:Boolean;

    //Speed
    var characterSpeed:Number = 15;

    //Main constructor
    public function main() 
    {
        //Array initializer
        missleArray = new Array();
        missle = new Missle();

        //Update events listeners.
        stage.addEventListener(Event.ENTER_FRAME, eventUpdated);

        //Update keyboard events listeners
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyUnpressed);
    }

    //Events functions
    //This functions updated everytime the object is move
    private function eventUpdated(e:Event):void 
    {
        playerMoving();
        playerClampMoving();
        checkMissleOffScreen();
    }

    private function checkMissleOffScreen():void 
    {
        for (var i = 0; i < missleArray.length; i++ )
        {
            var currentMissle:Missle = missleArray[i];

            if (currentMissle.y < 0)
            {
                missleArray.splice(i, 1);
                missle.destroyMissle();

            }
        }
    }

    private function playerClampMoving():void 
    {
        if (rect.x < 0)
        {
            rect.x = 0;
        }
        if (rect.x > stage.stageWidth - rect.width)
        {
            rect.x = stage.stageWidth - rect.width;
        }
        if (rect.y < 0)
        {
            rect.y = 0;
        }
        if (rect.y > stage.stageHeight - rect.height)
        {
            rect.y = stage.stageHeight - rect.height;
        }
    }

    private function playerMoving():void 
    {
        if (leftKeyIsDown == true)
        {
            rect.x -= characterSpeed;
        }
        if (rightKeyIsDown == true)
        {
            rect.x += characterSpeed;
        }
        if (upKeyIsDown == true)
        {
            rect.y -= characterSpeed;
        }
        if (downKeyIsDown == true)
        {
            rect.y += characterSpeed;
        }
        if (spaceKeyIsDown == true)
        {
            shootingMissle();
        }
    }

    private function shootingMissle():void 
    {
        missle = new Missle();
        missle.x = rect.x + (rect.width / 2);
        missle.y = rect.y;

        missleArray.push(missle);
        trace(missleArray.length);

        stage.addChild(missle);
    }

    //Keyboard functions
    //Check to see whether the user releases the keyboard
    private function keyUnpressed(e:KeyboardEvent):void 
    {
        if (e.keyCode == 37)
        {
            leftKeyIsDown = false;
        }
        if (e.keyCode == 39)
        {
            rightKeyIsDown = false;
        }
        if (e.keyCode == 40)
        {
            downKeyIsDown = false;
        }
        if (e.keyCode == 38)
        {
            upKeyIsDown = false;
        }
        if (e.keyCode == 32)
        {
            spaceKeyIsDown = false;
        }
    }

    //Check to see whether the user presses the keyboard
    private function keyPressed(e:KeyboardEvent):void 
    {
        if (e.keyCode == 37)
        {
            leftKeyIsDown = true;
        }
        if (e.keyCode == 39)
        {
            rightKeyIsDown = true;
        }
        if (e.keyCode == 40)
        {
            downKeyIsDown = true;
        }
        if (e.keyCode == 38)
        {
            upKeyIsDown = true;
        }
        if (e.keyCode == 32)
        {
            spaceKeyIsDown = true;
        }
     }

   }

}

这是 Missle 的课程:

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

/**
 * ...
 * @author test
 */
public class Missle extends Sprite 
{



    public function Missle() 
    {
        addEventListener(Event.ADDED_TO_STAGE, onAdd);
    }

    private function onAdd(e:Event):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, onAdd);
        //Objects are on the stage
        init();
    }

    private function init():void 
    {
        addEventListener(Event.ENTER_FRAME, missleLaunch);
    }

    private function missleLaunch(e:Event):void 
    {
        this.y -= 15;
    }

    public function destroyMissle():void
    {
        parent.removeChild(this);
        removeEventListener(Event.ENTER_FRAME, missleLaunch);

    }

   }

}
4

1 回答 1

1

试试这个:

public function destroyMissle():void
{
    if(parent !== null) parent.removeChild(this);
    removeEventListener(Event.ENTER_FRAME, missleLaunch);
}

有可能您.destroyMissile()不止一次调用parent,因为您已将其从舞台上移除并且它没有父级,因此含义可能为空。

于 2013-05-10T02:19:46.883 回答