Guest User

Untitled

a guest
Nov 12th, 2019
61
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class GameTimer
  2. {
  3. public GameTimer()
  4. {
  5. AnimationTimer timer = new AnimationTimer()
  6. {
  7. private long timestamp;
  8. private long time = 0;
  9. private long fraction = 0;
  10. final TextField display = new TextField("0");
  11.  
  12. @Override
  13. public void start()
  14. {
  15. // current time adjusted by remaining time from last run
  16. timestamp = System.currentTimeMillis() - fraction;
  17. super.start();
  18. }
  19.  
  20. @Override
  21. public void stop()
  22. {
  23. super.stop();
  24. // save leftover time not handled with the last update
  25. fraction = System.currentTimeMillis() - timestamp;
  26. }
  27.  
  28. @Override
  29. public void handle(long now)
  30. {
  31. long newTime = System.currentTimeMillis();
  32. if (timestamp + 1000 <= newTime) {
  33. long deltaT = (newTime - timestamp) / 1000;
  34. time += deltaT;
  35. timestamp += 1000 * deltaT;
  36. display.setText(Long.toString(time));
  37. }
  38. }
  39. };
  40. }
  41. }
RAW Paste Data