Sign in to follow this  
caymanbruce

How to monitor custom timer ticks in Phaser?

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

 

Share this post


Link to post
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.)

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.