2

对于我用 JavaScript 创建的第一个(正确的)游戏,我正在制作一个 Snake 游戏。我将有关蛇的所有信息存储在这样的类中:

var snake={

    //The width and height of each "segment" of the snake (in pixels)
    size : 20,


    /*Direction that the snake's moving in

    Left=0
    Right=1
    Up=2
    Down=3*/
    direction : rand_int_between(0,3),


    /*Positions of the snake's "segments" (1 is the snake's head, then 2 (when it's created) will be the next segment of the snake,
    then 3 will be the next, and so on*/
    positions : {
        1 : {
            "x" : rand_int_between(0, 35)*snake.size, //There are 36 possible "columns" that the snake can be in
            "y" : rand_int_between(0, 23)*snake.size
        }
    },


    //Will be set to False if the game hasn't started or of it's paused
    moving : false
}

//Alert(snake.size);

现在,由于某种原因,这破坏了我的代码。当我将随机整数乘以“snake.size”时,我已经指出它,因为如果我将这些行更改为简单地将它乘以 20,那么脚本就可以正常工作。

我有一种感觉,这是您一旦听到就无法相信自己错过了它的那些问题之一!

有人可以帮忙,因为这让我发疯,哈哈。我认为我没有错误地访问“size”属性,因为如果我取消注释该代码的最后一行,然后从位置属性中删除“snake.size”位,它会像你一样提醒“20”预计。

这是 rand_int_between():

function rand_int_between(min, max) { //Will include min and max
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
4

3 回答 3

6

snake声明对象时不能访问对象 ( ) 的元素。尝试移动 Positions 的声明:

var snake={

    //The width and height of each "segment" of the snake (in pixels)
    size : 20,


    /*Direction that the snake's moving in

    Left=0
    Right=1
    Up=2
    Down=3*/
    direction : rand_int_between(0,3),


    //Will be set to False if the game hasn't started or of it's paused
    moving : false
};


/*Positions of the snake's "segments" (1 is the snake's head, then 2 (when it's created) will be the next segment of the snake,
    then 3 will be the next, and so on*/
snake.Positions = {
    1 : {
        "x" : rand_int_between(0, 35)*snake.size, //There are 36 possible "columns" that the snake can be in
        "y" : rand_int_between(0, 23)*snake.size
    }
};
于 2013-11-11T20:48:17.080 回答
3

正如 Samy 解释的那样,您试图在构建对象时访问它,但这是不可能的。一种解决方法是使用外部变量作为大小(您可以将整个事物包装在立即执行的函数中以避免污染全局范围):

var snake = (function() {
    var size = 20;
    return {
       size : size,
       /* ... */
       positions : {
            1 : {
                "x" : rand_int_between(0, 35) * size,  
                "y" : rand_int_between(0, 23) * size
            }
       },
       /* ... */
    };
}());
于 2013-11-11T20:50:24.827 回答
-4

snkae尚未定义。我认为您需要的是其他类创建语法:

功能蛇(){

    //The width and height of each "segment" of the snake (in pixels)
    var size = 20;


    /*Direction that the snake's moving in

    Left=0
    Right=1
    Up=2
    Down=3*/
    var direction = rand_int_between(0,3);


    /*Positions of the snake's "segments" (1 is the snake's head, then 2 (when it's created) will be the next segment of the snake,
    then 3 will be the next, and so on*/
    var positions = {
        1 : {
            "x" : rand_int_between(0, 35)* size, //There are 36 possible "columns" that the snake can be in
            "y" : rand_int_between(0, 23)* size
        }
    };


    //Will be set to False if the game hasn't started or of it's paused
    var moving = false;
}

var 蛇 = 新蛇();

然后它工作。

于 2013-11-11T20:46:46.497 回答