var Snake = new Snake(250,250,-1,0);
Are you trying to overwrite the Snake
constructor here? Anyway, the declaration of the local variable is shadowing the global Snake
constructor reference, and Snake
is undefined
when you try to call it. Use a different variable name - usually instance names are lowercased:
var snake = new Snake(250, 250, -1, 0);
for(i=Snake.snakeBlock.length(); i>=0;i--){
- use a local variable:
var i
.length
is indeed not a function: it is a plain, numeric property.snakeBlock
is a local variable in theSnake
constructor, not a property.
var s = Snake.snakeBlock[i].block;
snakeBlock[i]
does already reference the Block
instance you put there. It does not have a .block
property.
var snakeBlock = [];
As already mentioned, this does declare a local variable. You want to access it from the outside, though (in prototype methods, in your drawSnake
call), so you should create a property instead:
this.snakeBlock = [];
Read more on the differences in Javascript: Do I need to put this.var for every variable in an object?.
Snake.prototype.create = function(){
Never put an assignment to the prototype inside the constructor! Your indentation is also screwed up.
snakeBlock.push(…)
Now, access it as a property:
this.snakeBlock.push(…)
Also see @MattWay's fiddle for a fixed up version :-)