Jump to content

How to monitor custom timer ticks in Phaser?


caymanbruce
 Share

Recommended Posts

I am using a custom timer to display the remaining time in the game.

In the Timer class I have a function to create a timer, and attach a tick function as its parameter. The timer is currently not working and I can't monitor the changes in the ticks. I put a console.log(myTime) inside the timerTick but nothing shows up.

 

constructor(options) {
    this.totalTime = options.seconds;
	this.game = options.game;
	this.onComplete = options.onComplete;
	const key = options.key || 'timer';
}

reset() {
	if (this.timer) {
		this.timer.stop();
	}

	this.hasFinished = false;
	this.timer = this.game.time.create();
	this.timer.repeat(Phaser.Timer.SECONDS, this.totalTime, this.timerTick.bind(this), this);
	this.timer.onComplete.addOnce(() => {
	    this.hasFinished = true;
	    if (this.onComplete) {
		    this.onComplete();
	    }
	});
}

remainingTime() {
	return this.totalTime - this.timer.seconds;
}

timerTick() {
	const myTime = this.remainingTime();
    console.log(myTime);
}

 

Link to comment
Share on other sites

4 hours ago, samme said:

Should be Phaser.Timer.SECOND. Then try game.debug.timer().

Thank you! That works! I have got another problem though. The current timer stops running when the browser window is unfocused. What should I do to make it continue to run even if I am not focusing on the game window? (For example I may switch to my editor's window when playing the game.)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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