Jump to content

Timer/Counter Reset On Game End


fedora
 Share

Recommended Posts

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

  • 2 weeks later...

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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