now im using
millis = System.currentTimeMillis() - startTime;
hours = millis / (1000 * 60 * 60);
millis -= hours * (1000 * 60 * 60);
minutes = millis / (1000 * 60);
millis -= minutes * (1000 * 60);
seconds = millis / 1000;
g.drawString("Time: " + hours + “:” + minutes + “:” + seconds, 300, 10);
is there a way to make it stop? for example press pause, the timer freezes, again pause and it works?
Sure.
You could have a boolean isPaused, and only update the clock when isPaused is false.
It depends. Do you want the clock to actually stop updating when you pause, or simply stop displaying updates?
Using a boolean called isPaused or similar is good.
You can then do something like:
public void update(int delta) {
if (isPaused) {
//update the pause menu
}
else {
//update the game
}
}
That code would just stop updating the game at its current position and then when it is un-paused, it will continue from where it was.
The rendering method would be similar but you would just draw the pause screen last and over the game.
public boolean paused;
public long pauseElapsed, pauseStarted;
public void pause(boolean on) {
paused = on;
if (on) {
pauseStarted = System.currentTimeMillis();
} else {
pauseElapsed += System.currentTimeMillis() - pauseStarted;
}
}
...
if (!paused) {
millis = System.currentTimeMillis() - startTime - pauseElapsed;
hours = millis / (1000 * 60 * 60);
millis -= hours * (1000 * 60 * 60);
minutes = millis / (1000 * 60);
millis -= minutes * (1000 * 60);
seconds = millis / 1000;
}
g.drawString("Time: " + hours + ":" + minutes + ":" + seconds, 300, 10);