Javascript游戏,数组和对象文字

时间:2012-11-01 18:19:34

标签: javascript arrays windows-8 object-literal

我正在使用HTML5和JavaScript开发适用于Windows 8的游戏。我有一个游戏的工作版本,它将“蟑螂”对象存储在一个数组中。我想把我的游戏代码放到微软的样本中(linked here)。在这个例子中,他们为游戏状态分解了一组对象文字,这些对象文字被保存并在各种类中使用:

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类,它保存了我之前引用的变量。

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命名空间下汇编,该命名空间从文件中调用每个类并在该命名空间下创建它们。因此,当用户启动游戏时,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 个答案:

答案 0 :(得分:0)

我有一个类似的问题,写入对象文字然后回读数据并得到错误。显然,这是两个实例中的灰色区域:

  1. 使用点表示法添加键/值对
  2. 异步访问对象文字
  3. 在第一种情况下,规范并不保证读操作能够正常工作。在第二种情况下,您必须通过以下方式处理访问:

    • 多次回调
    • 处理多种数据类型的回调

    您可以使用简单的一个衬垫进行测试:

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

    <强>参考