...

View Full Version : All variables are undefined in function called by Timer



DreamOfSleeping
07-07-2012, 07:23 PM
Hello everyone I'm Neil. I've been learning javascript for a few weeks now. I've found myself really stuck on something. I'm using the Timer. My problem is in the function that the Timer calls at each interval. For some reason all of the variables read undefined from that function. Oddly, with the exception of my screen variable.

This is more code than I probably should post, but I'm really not sure where the problem is. The startSequence method starts the timer and passes the flashSquare function. I've put an alert box in the flashSquare that displays if these variables are undefined and the alert does show.

Any help will be greatly appreciated.



MemoryGame = function() {
this.screen = new Screen();
this.score = new Score();
this.colors = new Array();
this.colorIndex = 0;
this.timerId;
this.showColor = true;
this.showingSequence = false;

MemoryGame.prototype.init = function() {
this.screen.drawTitle();
}

MemoryGame.prototype.startGame = function() {
clearInterval(this.timerId);
this.colors = new Array;
this.score.reset();
this.startSequence();
}

MemoryGame.prototype.startSequence = function() {
this.timerId = setInterval(this.flashSquare, 500);
this.showColor = true;
this.showingSequence = true;
this.colorIndex = 0;
}

MemoryGame.prototype.flashSquare = function() {

if (this.showColor == undefined && this.showingSquence == undefined
&& this.colorIndex == undefined && this.colors == undefined) {

alert("These are all undefined");
clearInterval(this.timerId); // this doesn't stop the timer though because it's undefined!
}
}
}

DreamOfSleeping
07-07-2012, 08:11 PM
Ok no need for anyone to reply. I've figured it now. I googled setInterval and scope and it seems I'm not the first to have this problem!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum