fedora Posted December 14, 2015 Share Posted December 14, 2015 In my code, I am using the following to create a timer that counts down:var counter = 60;create: function () {timerText = game.add.text(game.world.centerX, game.world.centerY, 'TIMER: 60', {font: '15px Arial', fill: '#FFFFFF', align: 'center'});timerText.anchor.setTo(0.5, 12.4);game.time.events.loop(Phaser.Timer.SECOND, this.updateCounter, this);}updateCounter: function () {counter -= 1;timerText.setText('TIMER: ' + counter);}, timeUp: function () {stateText.text="GAME OVER";stateText.visible = true; },The above works as expected -- my problem is that when the game ends and the user plays again the timer becomes glitchy showing at times negative numbers or starting at weird places .. I assume what I need to do is reset the time or detroy the timer at the game end function but I have been unsuccesful in figuring out how to do so -- Any guidance would be greatly appreciated --- I think it is probably something simple -- but I could be wrong --- Link to comment Share on other sites More sharing options...
maximilianoo Posted December 23, 2015 Share Posted December 23, 2015 Hi! I changed your code a bit to illustrate better the situation. Basically I added two states, put your code in playState (and variable counter in this state), when the time ends, it goes to gameOverState If you click anywhere in the screen it goes back to playState. (I put the conter to 5 just so I could test it quickly).Thanks for your code, just last night I was thinking how to do a timer countdown, now I know var playState = { create: function () { this.counter = 5; this.timerText = game.add.text(game.world.centerX, game.world.centerY, 'TIMER: 5', {font: '15px Arial', fill: '#FFFFFF', align: 'center'}); this.timerText.anchor.setTo(0.5, 0.5); game.time.events.loop(Phaser.Timer.SECOND, this.updateCounter, this); }, updateCounter: function () { this.counter -= 1; this.timerText.setText('TIMER: ' + this.counter); if (this.counter == 0) { game.state.start('gameOverState'); } }, timeUp: function () { stateText.text="GAME OVER"; stateText.visible = true; },};var gameOverState = { create: function () { this.counter = 60; this.stateText = game.add.text(game.world.centerX, game.world.centerY, 'GAME OVER', {font: '15px Arial', fill: '#FFFFFF', align: 'center'}); this.stateText.anchor.setTo(0.5, 0.5); game.input.onDown.addOnce(this.restartGame, this); }, restartGame: function () { game.state.start('playState'); },};var game = new Phaser.Game(500, 340, Phaser.AUTO, 'game');game.state.add('playState', playState);game.state.add('gameOverState', gameOverState);game.state.start('playState'); Link to comment Share on other sites More sharing options...
fedora Posted December 24, 2015 Author Share Posted December 24, 2015 Glad I was able to help .. a little ... I need to circle back to figure out why in mine it sometimes does not clear/refresh -- Link to comment Share on other sites More sharing options...
Recommended Posts