Zippy
Zippy

Reputation: 3887

Android: Counting time in my main loop

I'd like to a counter to run in the background while my game is being played so that when the player survives for say 1 minute, something happens.

Currently, I'm thinking of doing this by getting the system time at start (say in surfaceCreated) and then in my physics update method, getting the current time and comparing the two. When 60,000 ms have passed then I know obviously 1 minute has passed.

Is this the best way to do this? Or is there another/better/simpler way.

Thanks all

Upvotes: 0

Views: 167

Answers (1)

AStupidNoob
AStupidNoob

Reputation: 2050

That's a good way to go about it.

There's not many other options. In my game, I have a stopwatch that stops resets right before the frame update, so I can determine the amount of time spent doing other things. Then, each object is stepped with this amount of time.

It's good because it allows me to move each object at a speed relative to time rather than framerate, so if framerate differs on a different device, things still move at the same speed.

If you're not doing this, you really should be. If your step function looks something like

object.x += speed.x;

then it really should be

object.x += speed.x (in units/s) * step_time (in ms) / 1000.0

If you are doing this, it's probably best just to make a timer object that runs a Runnable when the timer expires (i.e it sums up the step-times and fires when it reaches 60sec).

Upvotes: 1

Related Questions