使用变量更改代码

时间:2015-12-09 10:39:39

标签: javascript

我曾尝试搜索论坛和谷歌,但如果以前已经回答过,那么我真的不知道要求道歉。

var currentLevel = 2;

var Level = function(layout, ColRow) {
  this.layout = layout;
  this.ColRow = ColRow;};

var level1 = new Level ([   1, 2, 1, 2, 2,
                            1, 1, 3, 1, 1,
                            1, 1, 1, 1, 1,
                            1, 1, 1, 1, 1],
                           [5, 4]);

var level2 = new Level ([   1, 2, 1, 2, 2,
                            1, 1, 3, 1, 1,
                            1, 1, 1, 1, 1,
                            1, 1, 1, 1, 1],
                           [2, 2]);                         


var drawTrack = function () {
id = 0;
for (var col = 0; col < level1.ColRow[0]; col ++) {
    tile[col] = [];
    for (var row = 0; row < level1.ColRow[1]; row++) {
        id = col + row*level1.ColRow[0];
        tile[col][row] = {x:0, y:0, type:"asphalt"};
        image (tiles[level1.layout[id]], col * 256, row * 256);
    }
}
};

在上面的示例中,我想将底部代码中的level1更改为level(currentLevel).ColRow [0]。 这可能还是有其他最佳做法来做到这一点?

*编辑 阵列解决方案解决了我的问题,谢谢! 我之前找到的建议链接,但无法看到它如何改变代码中的变量?也许我错过了什么。

1 个答案:

答案 0 :(得分:2)

如何创建一个级别数组?

$("footer").insertBefore("#myCarousel");

然后访问:

var levels = [

    new Level ([1, 2, 1, 2, 2,
    1, 1, 3, 1, 1,
    1, 1, 1, 1, 1,
    1, 1, 1, 1, 1],
    [5, 4]),

    new Level ([1, 2, 1, 2, 2,
    1, 1, 3, 1, 1,
    1, 1, 1, 1, 1,
    1, 1, 1, 1, 1],
    [2, 2])

];
相关问题