2013-05-10 106 views
-1

我正在创建一个射击游戏。当我决定在到达舞台顶部时创建一种方法去除导弹后,我遇到了我的代码问题。我可以在没有任何问题的情况下运行该程序,但是我已经意识到,如果我拿着射击按钮,导弹不会从舞台上移开。但是,如果我点击拍摄按钮,导弹将从输出中打印出错误#1009。Actionscript 3返回错误#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; 
     } 
    } 

    } 

} 

这里的导弹武器的类别:

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

    } 

    } 

} 

回答

1

试试这个:

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

这是一种可能性,您多次呼叫.destroyMissile()意味着parent将为空,因为您已将其从舞台中移除并且没有父项。