DrSpock Posted June 16, 2014 Share Posted June 16, 2014 Quick question about using the timer and loop in Phaser.I'm trying to get a program that runs a timer that checks how long the user has been playing the game, with the time displayed and it needs to be able to pause when a menu is opened.This is what I have currently: function create() { timer = game.add.text(250, 150, '0'); this.currentTimer = new Phaser.Timer(game, false); this.currentTimer.loop(Phaser.Timer.SECOND, this.updateTimer, this); this.currentTimer.start();}function update() { if(pauseButton.isDown){ timer.setText('Pause'); this.currentTimer.pause(); } if(resumeButton.isDown){ this.currentTimer.resume(); timer.setText(counter); test++; }}function updateTimer() { counter++; timer.setText(counter);}Using the console log I can tell that the timer is starting correctly, and can be paused and then resumed. However, the text does not update except to Pause when I press pause. Anyone know a solution to this? hollygood 1 Link to comment Share on other sites More sharing options...
lewster32 Posted June 16, 2014 Share Posted June 16, 2014 I think the problem may be here:this.currentTimer.loop(Phaser.Timer.SECOND, this.updateTimer, this);Which should probably be:this.currentTimer.loop(Phaser.Timer.SECOND, updateTimer, this);As an aside, it looks like you're calling pause and resume every frame that the buttons are held down; I'd probably do something like this to reduce all those unnecessary calls.function update() { if (pauseButton.isDown) { // only do this if the game is _not_ already paused if (!this.gamePaused) { this.currentTimer.pause(); timer.setText("Paused"); } this.gamePaused = true; } if (resumeButton.isDown) { // only do this if the game is already paused if (this.gamePaused) { this.currentTimer.resume(); } this.gamePaused = false; } // if the game isn't paused, update the text with the counter if (!this.gamePaused) { timer.setText(counter); }} Link to comment Share on other sites More sharing options...
DrSpock Posted June 17, 2014 Author Share Posted June 17, 2014 So I tried what you said, and it still doesn't update the counter. I'm thinking, for whatever reason that, the loop doesn't seem to be going off.The game is recognizing the timer state, it just isn't changing the counter. Any ideas? Link to comment Share on other sites More sharing options...
lewster32 Posted June 17, 2014 Share Posted June 17, 2014 Do console.log(counter) and ensure it's accessible within the updateTimer function. If it is, try doing counter.toString(10) as I've known problems with setText explicitly needing strings. Link to comment Share on other sites More sharing options...
DrSpock Posted June 17, 2014 Author Share Posted June 17, 2014 It doesn't seem that updateTimer is running at all. Console logs within it show nothing, and a console log of counter outside just stays at 0 the whole time. I think there's got to be some problem with the loop not starting for some reason. Here is the whole code if it helps.var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, updateTimer: updateTimer});function preload() {} var timer; counter=0;function create() { timer = game.add.text(250, 150, '0', { font: "64px Arial", fill: "#ffffff", align: "left" }); Phaser.inputEnabled = true; this.currentTimer = new Phaser.Timer(game, false); this.currentTimer.loop(Phaser.Timer.SECOND, updateTimer, this); this.currentTimer.start(); pauseButton = game.input.keyboard.addKey(Phaser.Keyboard.ENTER); resumeButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);}function update() { if (pauseButton.isDown) { // only do this if the game is _not_ already paused if (!this.gamePaused) { this.currentTimer.pause(); timer.setText("Paused"); } this.gamePaused = true; } if (resumeButton.isDown) { // only do this if the game is already paused if (this.gamePaused) { this.currentTimer.resume(); } this.gamePaused = false; } // if the game isn't paused, update the text with the counter if (!this.gamePaused) { timer.setText(''+counter); }}function updateTimer() { counter++; console.log(counter);} FinalFantasyVII 1 Link to comment Share on other sites More sharing options...
lewster32 Posted June 17, 2014 Share Posted June 17, 2014 Ok I've tracked down the problem. It seems creating a new Phaser.Timer isn't the way to do it, as it is not initialised properly. Instead change the timer creation line to this:this.currentTimer = game.time.create(false);And all should work as needed. The reason for this is that the game.time.create method adds the newly created timer to a private _timers array, which is updated by the core Time object. Without being included in this array, the timer is never updated. FinalFantasyVII and Madclaws 1 1 Link to comment Share on other sites More sharing options...
DrSpock Posted June 17, 2014 Author Share Posted June 17, 2014 Yes! Thank you very much! lewster32 1 Link to comment Share on other sites More sharing options...
Recommended Posts