对于我用 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;
}