2012-11-01 100 views
2

我正在爲HTML 8和JavaScript中的Windows 8開發遊戲。我有一個遊戲的工作版本,在數組中存儲「roach」對象。我試圖把我的遊戲代碼放入微軟的示例中(linked here)。在這個例子中,他們decalere一組遊戲狀態對象文本中保存和使用各種類:Javascript遊戲,數組和對象文字

internal: { 
    gamePaused: false, 
    gamePhase: "ready", 
    speed: 5, 
    score: 0, 
} 

在那裏,我想提出的陣列我的蟑螂對象。我以爲我可以這樣做:

roaches: [], 

但是當我打電話:

this.state.roaches.push(new Roach()); 
this.state.roaches.length; 

我得到一個:

JavaScript runtime error: Unable to get property 'length' of undefined or null reference 

什麼我做錯了,我該如何解決?

納特

附加信息:

好吧所以這裏是我的蟑螂類:

var Roach = WinJS.Class.define(function() { 
    this.position.x = Math.random() * gameCanvas.width + 1; 
    this.position.y = Math.random() * gameCanvas.height + 1; 
    this.squished = false; 
    this.image = GameManager.assetManager.assets.roachImage; 
}, 
{ 
    move: function (speed) { 
     // changes position 
    }, 
    squish: function() { 

    } 
}); 

這是包含其他類一個名爲game.js:

var Game = WinJS.Class.define(
    null, 
{ 
// other functions 
// Called when the game is being prepared to start 
    ready: function() { 
     // TODO: Replace with your own new game initialization logic 
     if (this.isSnapped()) { 
      this.state.gamePaused = true; 
     } else { 
      this.state.gamePaused = false; 
     } 
     this.state.gamePhase = "ready"; 
     switch (this.settings.skillLevel) { 
      case 0: 
       this.state.speed = 3; 
       break; 
      case 1: 
       this.state.speed = 5; 
       break; 
      case 2: 
       this.state.speed = 10; 
       break; 
     } 
     this.state.roaches.push(new Roach()); 
     this.state.score = 0; 
    }, 
// Main game render loop 
    draw: function() { 
     this.gameContext.clearRect(0, 0, gameCanvas.width, gameCanvas.height); 

     // TODO: Sample game rendering to be replaced 

     // Draw the current score 
     this.gameContext.fillStyle = "#FFFF99"; 
     this.gameContext.font = "bold 48px Segoe UI"; 
     this.gameContext.textBaseline = "middle"; 
     this.gameContext.textAlign = "right"; 
     this.gameContext.fillText(this.state.score, gameCanvas.width - 5, 20); 

     // update image positions 
     // THROWS ERROR HERE 
     for (var i = 0; i < this.state.roaches.length; i++) { 
      if (!this.state.roaches[i].squished) { 

      } 
     } 

     // Draw a ready or game over or paused indicator 
     if (this.state.gamePhase === "ready") { 
      this.gameContext.textAlign = "center"; 
      this.gameContext.fillText("READY", gameCanvas.width/2, gameCanvas.height/2); 
     } else if (this.state.gamePhase === "ended") { 
      this.gameContext.textAlign = "center"; 
      this.gameContext.fillText("GAME OVER", gameCanvas.width/2, gameCanvas.height/2); 
     } else if (this.state.gamePaused) { 
      this.gameContext.textAlign = "center"; 
      this.gameContext.fillText("PAUSED", gameCanvas.width/2, gameCanvas.height/2); 
     } 
    } 

現在在一個名爲gameState.js的文件中定義了一個gameState類,該類包含vari我之前提到過的那些。

var GameState = WinJS.Class.define(
    null, 
{ 
    config: { 
    // TODO: Adjust these values to configure the template itself 
     frameRate: 20, // Set to 0 to have no update loop at all 
     minSquished: 20, 
     currentPage: "/html/homePage.html", 
     gameName: "SDK Game Sample", // Used by share contract on scores page 
    }, 

    // TODO: Replace these public settings exposed on the settings panel 
    external: { 
     playerName: "Player", 
     soundVolume: 100, 
     skillLevel: 0, 
    }, 

    // TODO: Replace these values with state variables relevant for your game 
    internal: { 
     gamePaused: false, 
     gamePhase: "ready", 
     roaches: [], 
     speed: 5, 
     score: 0, 
    }, 

這些文件的命名空間遊戲管理器從文件調用每個類和命名空間下創建它們組裝之下。所以當用戶開始遊戲時,GameManager調用ready函數,然後調用draw函數來更新屏幕。希望這有助於澄清。

命名空間定義如下:

var game = new Game(); 
var touchPanel = new TouchPanel(); 
var state = new GameState(); 
state.load(); 

WinJS.Namespace.define("GameManager", { 
     navigateHome: navigateHome, 
     navigateGame: navigateGame, 
     navigateRules: navigateRules, 
     navigateScores: navigateScores, 
     navigateCredits: navigateCredits, 
     showPreferences: showPreferences, 
     onBeforeShow: onBeforeShow, 
     onAfterHide: onAfterHide, 
     game: game, 
     state: state, 
     assetManager: assetManager, 
     scoreHelper: scoreHelper, 
     gameId: gameId, 
     touchPanel: touchPanel 
    }); 
+1

[] =陣列,{} =對象 –

+6

這很奇怪...如果'this.state.roaches.push(新Roach());'不會拋出錯誤,那麼'this.state.roaches.length;'不應該。但是我們沒有足夠的信息來幫助你。我們需要更多的上下文,特別是你使用'this.state.roaches'做什麼。 –

+0

已編輯。謝謝您的幫助! – nat45928

回答

0

我有寫一個類似的問題提出異議文字,然後讀回的數據,卻得到了錯誤。顯然,這是一個灰色區域中的兩個實例:

  1. 的鍵/值對是使用點符號
  2. 對象常量被異步訪問

在第一種情況,該規範沒有按加入不保證讀取操作能夠正常工作。在第二種情況下,你必須處理通過訪問:

  • 多個回調
  • 它處理的回調多種數據類型

你可以用一個簡單的襯墊測試:

var foo = {}; foo["bar"] = []; setTimeout(function(){foo.bar.push(new Date); console.log('Hi ' + foo.bar.length)}, 5000);

參考