For my first (proper) game created in JavaScript I'm making a Snake game. I'm storing all of the information about the snake in a class like this:
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);
Now this for some reason breaks my code. I've pinpointed it to when I multiply the random integer by "snake.size", because if I change those lines to simply multiplying it by 20 then the script works fine.
I'm getting the feeling that this is one of those questions where you won't be able to believe you missed it once you hear it!
Can someone please help because this is driving me insane, haha. I don't think I'm accessing the "size" property incorrectly because if I uncomment the last line of that code, and then remove the "snake.size" bit from the positions property, it alerts "20" as you'd expect.
Here is rand_int_between():
function rand_int_between(min, max) { //Will include min and max
return Math.floor(Math.random() * (max - min + 1)) + min;
}