I'm making a simple timer code that updates a variable every second, and repaints the screen with the correct second. However, if the game "ends", I want to stop the timer. If the game restarts, I'm resetting "seconds" to 0, however, it doesn't appear to wait a full second before running again. I'm not quite sure why.
public void startTimer()
{
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
if(endGame == false)
{
seconds++;
repaint();
}
}
}, 0, 1000);
}
The code is a modified form of what's seen here. I have another method resetting seconds to 0 when I want the timer to restart.
Question
simcityfreak4
I'm making a simple timer code that updates a variable every second, and repaints the screen with the correct second. However, if the game "ends", I want to stop the timer. If the game restarts, I'm resetting "seconds" to 0, however, it doesn't appear to wait a full second before running again. I'm not quite sure why.
public void startTimer() { Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void run() { if(endGame == false) { seconds++; repaint(); } } }, 0, 1000); }The code is a modified form of what's seen here. I have another method resetting seconds to 0 when I want the timer to restart.
Thanks for the help.
Link to comment
Share on other sites
1 answer to this question
Recommended Posts