java.util.Timer - how to use properly?
I need a timer for my game: when the user taps the screen, timer should pause and resu开发者_StackOverflow中文版me. Firstly, I tried to create a Timer
once, MyTimerTask.cancel()
to pause it, Timer.schedule(new MyTimerTask, ...)
to resume it. But task manager shows that when the timer is paused, my application spends a huge amount of CPU (I develop under Android). As I understand, Timer
executes empty queue. Now I .cancel()
and .purge()
the Timer
and re-create it, along MyTimerTask
. Is it right way to use Timer
? Or I don't understand something?
You could just add an if statement in you main game loop like:
while(runMainLoop)
{
if(gameIsNotPaused)
{
updatePhysics();
doDraw();
}
}
And then when the user presses the pause button, you just set gameIsNotPaused to false/true.
精彩评论