0
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ So the problem is that per tick you want to add more than one to the total? Wouldn't adjusting the coinsToDisplay++ line work? \$\endgroup\$ Commented Feb 8, 2015 at 17:56
  • \$\begingroup\$ Yeah @Seth Battin the problem was I couldn't work out how to get the amount that should be drawn, But Byte56's answer helped me understand it - cheers \$\endgroup\$ Commented Feb 9, 2015 at 20:51

1 Answer 1

1
\$\begingroup\$

Change your counting to be more than one at a time:

Instead of:

coinsToDisplay++

Do something a bit more dynamic like:

coinsToDisplay += coinsPlayerHas/100;
if(coinsToDisplay > coinsPlayerHas)
    coinsToDisplay = coinsPlayerHas;

In this case (if the player has more than 100 coins) it'll always take around 100 ticks to count to the total.

You can modify it further by using a float for the amount to add, and only displaying an integer amount. That'll make it so the time from 0 to coinsPlayerHas will always be the same.

\$\endgroup\$
1
  • \$\begingroup\$ Yep @Byte56, this is just what I needed, I was thinking about it all wrong, trying to dynamically change the delay duration rather than the amount of coins that needed to be drawn - cheers \$\endgroup\$ Commented Feb 9, 2015 at 20:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .