我是否错误地访问JavaScript对象(或者为什么这不起作用)?

2 javascript debugging properties object

对于我用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);
Run Code Online (Sandbox Code Playgroud)

现在这出于某种原因打破了我的代码.当我将随机整数乘以"snake.size"时,我已经确定了它,因为如果我将这些行改为简单地乘以20那么脚本工作正常.

我感觉这是其中一个问题,一旦你听到它,你将无法相信你错过了它!

哈哈,有人可以帮助,因为这让我疯了.我不认为我正在错误地访问"大小"属性,因为如果我取消注释该代码的最后一行,然后从位置属性中删除"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;
}
Run Code Online (Sandbox Code Playgroud)

sro*_*oes 6

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),


    //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
    }
};
Run Code Online (Sandbox Code Playgroud)