Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Timer issues: Cannot call 'loop' of undefined?


RyanTheBoy
 Share

Recommended Posts

EDIT: For those still interested in this issue I have revived it with updated code and such here

 

Heya, this is my first post so I apologize if this has already been covered in another relevant topic. 

 

Essentially I am setting up a HUD prototype. I pass the game object to the HUD initially then prototype all of the relevant functions afterword. 

 

I am trying to get a timer setup to essentially display a 1 minute countdown. Each time I try to run the game though I get the error...

 

Uncaught TypeError: Cannot call method 'loop' of undefined

 

Here is the code I am using...

~~~

HUD = function(game) {
this.game = game;
this.score = 0;
this.text = null;
//this.timer = null;
};
 
var countDown = 60;
 
HUD.prototype = {
create: function() {
this.text = this.game.add.text(16, 16, 'Score: 0\nTimer: 01:00', { fontSize: '32px', fill: '#000' });
this.game.time.events.loop(Phaser.Timer.SECOND, updateText, this.game);
},
 
update: function() {
 
}
};
 
function updateText() {
countDown--;
 
if (countDown < 10) {
this.text.content = 'Score: 0\nTimer: 00:0' + countDown;
} else {
this.text.content = 'Score: 0\nTimer: 00:' + countDown;
}
 
if (countDown == 0) {
endGame();
}
}
 
function endGame() {
game.state.start('GameOver');
}
~~~
Any thoughts? 
Link to comment
Share on other sites

Yes I think this is related to a bug I (mostly) fixed last night - when the state changes all of the timers are cleared, but as that is happening in a callback here it's still processing the list of timers, so throws that error. To get around it for now instead of changing state, set a "willChange" flag or something and check that in your state update. If true, then change. This will remove the state change outside of the callback.

Link to comment
Share on other sites

I am still getting the same error it seems. Here is the updated code..

 

~~~

HUD = function(game) {
this.game = game;
this.score = 0;
this.text = null;
//this.timer = null;
};
 
var countDown = 60;
var endGame = false;
 
HUD.prototype = {
create: function() {
this.text = this.game.add.text(16, 16, 'Score: 0\nTimer: 01:00', { fontSize: '32px', fill: '#000' });
this.game.time.events.loop(Phaser.Timer.SECOND, updateText, this.game);
},
 
update: function() {
if (endGame) {
this.game.state.start('GameOver');
}
}
};
 
function updateText() {
countDown--;
 
if (countDown < 10) {
this.text.content = 'Score: 0\nTimer: 00:0' + countDown;
} else {
this.text.content = 'Score: 0\nTimer: 00:' + countDown;
}
 
if (countDown == 0) {
endGame = true;
}
}
~~~
 
Perhaps I am misunderstanding what you had meant. =/
Link to comment
Share on other sites

No that's exactly what I meant, it must be a bug in the Timer. Please keep an eye on the github repo (you can set it to 'watch' if you like) and you'll see when I push a new build to address this. You could try the dev branch (1.1.5) as it has a few Timer fixes in already and no other changes, so is safe to use atm.

Link to comment
Share on other sites

  • 2 weeks later...

I recently started to use generator for phaser with Yeoman. npm : https://www.npmjs.org/package/generator-phaser

 

When swaping my code to this new project architecture, I had the same problem as described above.

 

So I checked the version, it was 1.3. I replaced by 1.5, and it solved the issue !

 

Thanks Rich for the 1.5 I was stunned to see it comes out so quickly after the 1.1.4, keep up the good job.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...