简体   繁体   中英

java.util.Timer - how to use properly?

I need a timer for my game: when the user taps the screen, timer should pause and resume. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM