[Solved] How do I build an animated counter that adds hundreds or thousands of points to a score display?

Hello,

My game has rounds that accumulate points. After each round, I’d like to add the new points to the beginning score in an animated fashion much like a pinball table adds points as the ball strikes things.

I want the numbers to count up (or down) kind of in a blurr and then end on the final score.

I first just used performWithDelay and set the iteration value to the number of points to add or subtract.

  
local function incrementScore ( event )  
 totalScore = totalScore + sessionScore  
 totalScoreValue.text = totalScore  
end  
timer.performWithDelay(20, incrementScore, sessionScore )  

The problem with this is that the sessionScore can range from hundreds to thousands. If I’m adding 2 thousand points, it takes too long.

I then did this

local function incrementScore ( event )  
 totalScore = totalScore + sessionScore/100  
 totalScoreValue.text = totalScore  
end  
timer.performWithDelay(20, incrementScore, 100 )  

which gives a consistent overall time but the final value is sometimes off due to the fractions being added to the total score.

My current solution uses the above function in conjunction with a second performWithDelay that is tweaked to delay until just after the previous one finishes and it calls another function to set the totalScore to the final, correct, value. It’s a slimy hack and I need something better.

Anybody solve this one yet?

Thanks,

Tom

[import]uid: 81264 topic_id: 14887 reply_id: 314887[/import]

try using
[lua]totalScore = totalScore + math.round(sessionScore/100)[/lua]

See the sample below
[lua]local totalScoreValue = display.newText(’’,50,50,nil,25)
totalScore = 2000
sessionScore = 1009

local function incrementScore ( event )
totalScore = totalScore + math.round(sessionScore/100)
totalScoreValue.text = totalScore
end

timer.performWithDelay(20, incrementScore, 100 )[/lua] [import]uid: 71210 topic_id: 14887 reply_id: 54977[/import]

When you add 100’s and 1000’s do you want the player to see the score being added or just provide an illusion of a counter?

You can create a blurry animation of a counter moving from 0 to 9 and back, then play that animation for a period of time that is proportional to the magnitude of your score, it will give the effect of the counter being moved, where you will have each digit at a different speed to match up with the rolling.

otherwise, the same above is fine, as it will show the score increasing.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 14887 reply_id: 54998[/import]

Thanks for the replies.

The use of math.round doesn’t work for me (and I wouldn’t think it would anyway). Rounding the values loses data of course. In the above example, the final result I get is 3000, not 3009 as it should be.

The animation it provides is fine but the final result is non-deterministic.

Tom [import]uid: 81264 topic_id: 14887 reply_id: 55213[/import]

Solved this one well enough I think.

Basically, I keep track of how many times my increment counter is called and on the last call by performWithDelay, I set the final score, overriding any rounding errors from math.round

  
 local function incrementScore ( event )  
 totalScore = totalScore + math.round(sessionScore/100)  
 totalScoreValue.text = totalScore  
 animateScoreCounter = animateScoreCounter + 1  
 if animateScoreCounter == 100 then  
 setFinalScore()  
 end  
 end  
  

My setFinalScore function just overrides the score value with saved initial score and session score values.

Would be nicer if performWithDelay would pass a counter but making my own isn’t that bad.

Tom [import]uid: 81264 topic_id: 14887 reply_id: 55227[/import]

an even simpler approach will be :

[code]

local totalScoreValue = display.newText(’’,50,50,nil,25)
totalScore = 2000
sessionScore = 1009

local function incrementScore ( event )
totalScore = totalScore + sessionScore/100
totalScoreValue.text = math.round(totalScore)
end

timer.performWithDelay(20, incrementScore, 100 )

[/code] [import]uid: 88119 topic_id: 14887 reply_id: 57305[/import]

(I am eleven years old and a novice at Corona)

Very useful, though when I create extremely high numbers (100,000,000), it is generally off by a considerable amount, and in any case, it counts somewhat slowly. But it is good in both adding score and subtracting score (just make the incrementScore a negative number). Glad I found this! [import]uid: 82408 topic_id: 14887 reply_id: 57322[/import]