Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GameTimer
- {
- public GameTimer()
- {
- AnimationTimer timer = new AnimationTimer()
- {
- private long timestamp;
- private long time = 0;
- private long fraction = 0;
- final TextField display = new TextField("0");
- @Override
- public void start()
- {
- // current time adjusted by remaining time from last run
- timestamp = System.currentTimeMillis() - fraction;
- super.start();
- }
- @Override
- public void stop()
- {
- super.stop();
- // save leftover time not handled with the last update
- fraction = System.currentTimeMillis() - timestamp;
- }
- @Override
- public void handle(long now)
- {
- long newTime = System.currentTimeMillis();
- if (timestamp + 1000 <= newTime) {
- long deltaT = (newTime - timestamp) / 1000;
- time += deltaT;
- timestamp += 1000 * deltaT;
- display.setText(Long.toString(time));
- }
- }
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement