[英]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.首先,我尝试创建一个Timer
一次, MyTimerTask.cancel()
暂停它, Timer.schedule(new MyTimerTask, ...)
恢复它。 But task manager shows that when the timer is paused, my application spends a huge amount of CPU (I develop under Android).但是任务管理器显示,当定时器暂停时,我的应用程序消耗了大量的CPU(我在Android下开发)。 As I understand, Timer
executes empty queue.据我了解, Timer
执行空队列。 Now I .cancel()
and .purge()
the Timer
and re-create it, along MyTimerTask
.现在我.cancel()
和.purge()
Timer
并沿着MyTimerTask
重新创建它。 Is it right way to use Timer
?使用Timer
是否正确? Or I don't understand something?还是我不明白什么?
You could just add an if statement in you main game loop like:您可以在主游戏循环中添加一个 if 语句,例如:
while(runMainLoop)
{
if(gameIsNotPaused)
{
updatePhysics();
doDraw();
}
}
And then when the user presses the pause button, you just set gameIsNotPaused to false/true.然后当用户按下暂停按钮时,您只需将 gameIsNotPaused 设置为 false/true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.