2013-10-24 78 views
0

我正在做一個平臺遊戲。我只是爲了讓玩家在舞臺上四處移動,並且能夠跳躍,並添加了某種類型的重力。但是,當我運行它時,出現此錯誤:錯誤#1009:無法訪問空對象引用的屬性或方法。空對象錯誤;無法訪問它 - AS3

package { 

import flash.display.MovieClip; 
import flash.events.MouseEvent; 
import flash.events.KeyboardEvent; 
import flash.events.Event; 
import flash.ui.Keyboard; 

public class Code1 extends MovieClip { 

    var charSpeed:int = 0; 
    var velocity:int = 0; 
    var gravity:Number = 1; 
    var Jump:Boolean = false; 
    var leftKey:Boolean; 
    var rightKey:Boolean; 
    var upKey:Boolean; 

    private var platform:Platform; 

    public function startGame(){ 
     stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown); 
     stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp); 
     stage.addEventListener(Event.ENTER_FRAME, loop); 
     stage.addEventListener(Event.ENTER_FRAME, update); 
    } 

    public function Code() { 
    } 

    public function update(evt:Event){ 
     moveChar(); 
    } 

    public function moveChar(){ 
     if (leftKey == true){ 
      charSpeed -= 10; 
     } 
     if (rightKey == true){ 
      charSpeed += 10; 
     } 
     if (upKey == true){ 
      if(!Jump){ 
       velocity -= 14; 
       Jump = true; 
      } 
     } 
    } 


    function checkKeyDown(evt:KeyboardEvent){ 
     if (evt.keyCode == Keyboard.UP){ 
      upKey = true; 
     } 
     else if (evt.keyCode == Keyboard.RIGHT){ 
      rightKey = true; 
     } 
     else if (evt.keyCode == Keyboard.LEFT){ 
      leftKey = true; 
     } 
    } 

    function checkKeyUp(evt:KeyboardEvent){ 
     if (evt.keyCode == Keyboard.UP){ 
      upKey = false; 
     } 
     else if (evt.keyCode == Keyboard.RIGHT){ 
      rightKey = false; 
     } 
     else if (evt.keyCode == Keyboard.LEFT){ 
      leftKey = false; 
     } 
    } 

    function loop(evt:Event){ 
     player.x = charSpeed; 
     if (player.x < 0){ 
      player.x = 0; 
     } 
     if (player.x > 550){ 
      player.x = 550; 
     } 

     velocity += gravity; 

     if (!platform.hitTestPoint(player.x, player.y, true)){ 
      player.y += velocity; 
     } 

     for (var i = 0; i < 10; i++){ 
      if (platform.hitTestPoint(player.x, player.y, true)){ 
       player.y--; 
       velocity = 0; 
       Jump = false; 
      } 
     } 
    } 
} 

}

我的平臺聯動的「平臺」,但我設置了一個變量它(或嘗試)。我調試了代碼,它出現了這一行:player.x = charSpeed; 我不知道該怎麼做,如果有人可以幫助,那會很好。

回答

0

你永遠不會聲明或實例化(即player = new Player())你的播放器。
或者,如果您的播放器處於.fla時間軸的舞臺上,則需要實例名稱「player」。這可以在對象屬性中設置。

0

您的播放器對象爲空。

我沒有看到istantiation路線爲:

var player:Player = new Player(); 

添加之前使用播放器的屬性

相關問題