Sign in to follow this  
DrSpock

Pausing and Resuming Timer

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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);  }}

Share this post


Link to post
Share on other sites

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);}

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.