In my game the player can collect a 'virtual currency' - on the main menu I display the amount the player has collected. However, instead of simply displaying it, I want to count up from 0 to the amount the player has and then stop.
So - the logic would look something like this:
if (coinsToDisplay<=coinsPlayerHas){
coinsToDisplay++
}
And then I would simply render the amount to the screen.
The problem is that obviously the more coins the player has the longer it takes to count.
I need to find a way make sure the counting always takes an arbitary amount of time (lets say 2 seconds).
My logic is called 60 times per second. I know that I can introduce a delay to slow down the couning if the user has a very small amount of coins. The time to wait between each count would be:
delay = 2000/coinsPlayerHas; //2000 being 2 seconds expressed as milliseconds
I then wouldn't update the amount until the delay had passed.
if (delayHasExpired()){ //Only run this once our timer passed
if (coinsToDisplay<=coinsPlayerHas){
coinsToDisplay++
resetDelay();
}
}
But what if the player has thousands of coins? At 60 Ticks Per Second the most I could count when incrementing 1 per tick is 120 max in 2 seconds. Therefore I need to find a way of counting more than 1.
I just can't figure it out, so any pointers would be appreciated.
coinsToDisplay++
line work? \$\endgroup\$