Jump to content

Counting score on game over - fast


Recommended Posts

On game over I want to count score from 0 to any score user has won with the sound while counting, and it should finish fairly quickly, about few seconds no matter how big the score is.
I've seen some games built in phaser that have this counting, but with minified source to be able to see how they did it.
Here's jsfiddle of what I am able to come up with (it needs more adjustments to what I need), but I don't know if this is the way how they did it or is there a better way?

Oh, and it should be reusable to change to another Phaser text object (game.add.text) for counting.

Any information would be appreciated.

Link to comment
Share on other sites

I would decide how long you want it to take the score to increment, and do something like this in your update loop. You can set your timer_length to whatever you want, and the displayed score will always take that long to increment. 

var user_score = 9999999; // Or whatever the user scored.
var display_score = 0; // Start the displayed score at 0
var timer_length = 1000; // 1 second from now. Adjust as needed.
var start_time = this.game.time.time; // Save the current time
var timer = start_time + timer_length;

function update() {
	var now = this.game.time.time;
	// Check the timer every frame
	if (timer > now) {
		// The percentage of time that has ellapsed
		var score_factor = (now - start_time) / timer_length;

		// This should update the displayed user score. Every frame it will increment relative to how much time has ellapsed
		display_score = user_score * score_factor;
	else {
		// If time is up, ensure that the display score now displays the entire score by the user
		display_score = user_score;


Link to comment
Share on other sites

Tweening never came up to me to use it for counting. Elegant with minimal code.

But I would have to adjust both of the examples and synchronize sound with counting. For big scores is not a problem, but for small ones it is.
They would slowly count up and sound needs to follow that.

Thanks Zeterain and Tom for your examples.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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