开发者

java.util.Timer - how to use properly?

开发者 https://www.devze.com 2023-03-17 06:53 出处:网络
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.sch

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消