Trying to implement a timer for my game that I’m making. I have a backend class Game that has an int timeElapsed() method. This gives me elapsed time since the start in milliseconds. I also have a front end view that needs an update of this elapsed time. What is the best way to synchronize these two values? I could set up a daemon to call game.timeElapsed() every second, but it seems that there must be a better way to do this.
Another idea I had was to put some timekeeping in the frontend, but this seems like a violation of separation of responsibilities.
The Stopwatch class that I’m using to keep track of time does not support callbacks.
Thoughts?
Use the
java.util.Timerclass, to periodically update the view. Callgame.timeElapsed()in each iteration, don’t keep twoStopwatch-objects around.