2013-05-10 84 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將爲空,因爲您已將其從舞臺中移除並且沒有父項。