RickyD Posted May 18, 2015 Posted May 18, 2015 Any snippets to help with a countdown and a way to reset the counter when a boolean trips (or you can use a method like resetCountdown())
fixthissite Posted May 21, 2015 Posted May 21, 2015 class PaintTimer { private static final long NANO_SECOND = 1000000000L, BOUNDS = NANO_SECOND * 2; private long previousFrameTime; private long timeElapsed; private int secondsPassed; public int countDownUntil(boolean condition) { if(condition) secondsPassed = 0; long currentFrameTime = System.nanoTime(); timeElapsed += currentFrameTime - previousFrameTime; previousFrameTime = currentFrameTime; if(timeElapsed > BOUNDS) timeElapsed = 0; else if(timeElapsed >= NANO_SECOND) { secondsPassed++; timeElapsed = 0; } return secondsPassed; } } Your script would look something like: final class MyScript { private PaintTimer timer; public void onStart() { timer = new PaintTimer(); } public void onPaint(Graphics2D g) { int secondsPassed = timer.countDownUntil(getCondition()); //use secondsPassed } private boolean getCondition() { return ...; //your condition } }
fixthissite Posted May 21, 2015 Posted May 21, 2015 (edited) Accidental repose - Ignore this message Edited May 21, 2015 by fixthissite
RickyD Posted May 23, 2015 Author Posted May 23, 2015 class PaintTimer { private static final long NANO_SECOND = 1000000000L, BOUNDS = NANO_SECOND * 2; private long previousFrameTime; private long timeElapsed; private int secondsPassed; public int countDownUntil(boolean condition) { if(condition) secondsPassed = 0; long currentFrameTime = System.nanoTime(); timeElapsed += currentFrameTime - previousFrameTime; previousFrameTime = currentFrameTime; if(timeElapsed > BOUNDS) timeElapsed = 0; else if(timeElapsed >= NANO_SECOND) { secondsPassed++; timeElapsed = 0; } return secondsPassed; } } Your script would look something like: final class MyScript { private PaintTimer timer; public void onStart() { timer = new PaintTimer(); } public void onPaint(Graphics2D g) { int secondsPassed = timer.countDownUntil(getCondition()); //use secondsPassed } private boolean getCondition() { return ...; //your condition } } Thanks, it works now!