我在 AS3 中制作了一个游戏,我有 2 个文件。一种是 HWMain 和 HWGame。当我单击开始按钮时,脚本从 HWMain 切换到 HWGame,但出现此错误。
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at HWGame()
at MethodInfo-26()
at MethodInfo-25()
这是我的代码。
public class HWGame extends MovieClip
{
var INIT_STATE:String = "INIT_STATE";
var READY_STATE:String = "READY_STATE";
var PLAYER_STATE:String = "PLAYER_STATE";
var PLAY_STATE:String = "PLAY_STATE";
var END_STATE:String = "END_STATE";
var gameState:String;
//And another variable
public function HWGame()
{
gameState = INIT_STATE;
trace(gameState);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
function gameLoop(e:Event):void
{
switch (gameState)
{
case INIT_STATE :
initGame();
break;
case READY_STATE :
ready();
break;
case PLAYER_STATE :
startPlayer();
break;
case PLAY_STATE :
playGame();
break;
case END_STATE :
endGame();
break;
}
}
function initGame():void
{
//I write the long code
}
function ready():void
{
//I write the long code
}
function startPlayer():void
{
//I write the long code
}
function playGame():void
{
//I write the long code
}
function endGame():void
{
//I write the long code
}
}
}
我尝试修复它,我认为错误出现在 gameState = INIT_STATE。我该怎么办?
谢谢。