Using JavaScript functions as "classes": What am I doing wrong here? -
here's javascript "class" far:
function snake(c, c_h, c_w) { this.linksize = 10; // size of snake unit, in pixels /* on instantiation, snake direction down , has 1 link */ this.dy = this.linksize; this.dx = 0; this.link = c.rect(c_h/2, c_w/2, this.linksize, this.linksize); this.link.attr("fill", "#d7a900"); this.body = [link]; /* event listener changing direction of snake arrow keys on keyboard */ this.redirect = function(dirnum) { switch (dirnum) { /* dirnum corresponds 1 ---> right 2 ---> down 3 ---> left 4 ---> */ case 1: this.dx = this.linksize; this.dy = 0; break; case 2: this.dx = 0; this.dy = this.linksize; break; case 3: this.dx = -this.linksize; this.dy = 0; break; case 4: this.dx = -this.linksize; this.dy = 0; break; default: // never happens break; } } this.move = function() { /* ///// */ var temp = body[0]; body[0].translate(this.dx, this.dy); (var = 1, j = body.length; < j; ++i) { body[i] = temp; temp = body[i]; } } setinterval(this.move()); }
i have 2 problems being reported in google chrome console:
- uncaught referenceerror: link not defined (on
this.link.attr("fill", "#d7a900");
line). uncaught typeerror: undefined not function
(whenever press arrow key)
does have insight why these problems occurring? using this.
keyword correctly?
uncaught referenceerror: link not defined
mean call c.rect()
returns undefined
or null
.
"uncaught typeerror: undefined not function
(whenever press arrow key)"
there no event handling visible in code, might follow-up error.
triple check (using console.log
) c
correctly passed function , return value of c.rect()
is.
Comments
Post a Comment