-1
當我運行我的代碼時出現此錯誤:TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。類參考可能?
TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。在腳本:: Enemy/moveToward()
我假設它與在舞臺上註冊他的球員有關。到目前爲止,我有3個課程:引擎,玩家和敵人。我現在正在製作一款雙向滾動遊戲。
package scripts
{
import flash.display.*;
import flash.events.*;
import flash.events.Event;
import flash.display.Stage;
public class Enemy extends MovieClip
{
var enemySpeed: Number = 3;
var xDistance: Number = 50
var yDistance: Number = 20;
public var player: Player;
public function Enemy()
{
this.addEventListener(Event.ADDED_TO_STAGE, addListeners)
}
public function addListeners(event: Event):void
{
stage.addEventListener(Event.ENTER_FRAME, moveToward);
trace("handeled");
}
function moveToward(event: Event):void
{
if(this.x < player.x - xDistance)
{
this.x += enemySpeed;
this.scaleX = -1;
}
if(this(root).x > player.x+xDistance)
{
this.x -= enemySpeed;
this. scaleX = 1;
}
if(this.y < player.y-yDistance)
{
this.y += enemySpeed;
}
if(this.y > player.y+yDistance)
{
this.y -= enemySpeed;
}
}
在'moveToward()'的頂部添加'trace(「Player:」,player)''。我敢打賭它會打印出null(或者什麼也不是)。 – Florent 2012-07-10 12:15:18