0

我实际上是在尝试检查游戏的分数是否为高分。然后如果是,分数将被添加到排行榜中。但是我收到了这个错误:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at FlashGame_fla::MainTimeline/checkBananaHS()

在我的游戏中,在那个特定的帧中,这是链接到 checkBananaHS() 的代码:

function rpslsWon():void 
{
    gameOverBananaMC.visible = true;
    bananaWinLose.visible = true;
    bananaWinLose.text = "Kampai " + cPlayerData.pName + "! You are totally bananas!! \nYour Score: " + pBananaScore;
    toMenuBtn.visible = true;
    rollAgainBtn.visible = true;
    toMenuBtn.addEventListener(MouseEvent.CLICK, Click_backtoMain);
    rollAgainBtn.addEventListener(MouseEvent.CLICK, backtoBanana);
    saveItBtn.addEventListener(MouseEvent.CLICK, checkBananaHS);
    cPlayerData.pBananaScore = pBananaScore;
    saveData();
    tracePlayerData();
}

这是高分帧中的一段代码:

var rpslsHighScore:int;
var rpslsHSName:String;
rpslsHighScore = 0;
rpslsHSName = "";

//rpslsHighScore = 0;
bananaWinnerDisplay.text = " ";
bananaScoreDisplay.text = "0";

function checkBananaHS(event:MouseEvent):void
{
    if ((cPlayerData.pBananaScore > rpslsHighScore || 
    rpslsHighScore == 0) && cPlayerData.pBananaScore > 0)
    {
        trace("There's a new high score for Banana");
        rpslsHighScore = cPlayerData.pBananaScore;
        rpslsHSName = cPlayerData.pName;
        bananaScoreDisplay.text = "" + rpslsHighScore;
        bananaWinnerDisplay.text = rpslsHSName;
        saveData();
    }

}

我只是无法修复错误。谁能帮我吗?非常感谢!

4

1 回答 1

0

您的其中一个变量在checkBananaHS()调用时未设置。因此,当您尝试访问该对象的属性时,它会出错,因为什么都不存在。

请记住,这意味着您的函数中尚未设置以下对象之一:

  • cPlayerData
  • bananaScoreDisplay
  • bananaWinnerDisplay

在每一个上运行一个跟踪,在你的函数开始时一次一个,看看哪个没有返回[Object object]

于 2013-10-24T17:31:18.190 回答