your fps variable appeared out of nowhere.
or do you mean int fps = 1000/60?
If you don't care about an exact fps, this works:
then multiply velocities etc by the deltaTime variable.
deltaTime should be calculated before the processing so you can use it in the current frame.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| public void run() { long lastFPS = 0; int fps = 0; int fpsCount = 0;
long lastTime = System.currentTimeMillis(); while (running) { long time = System.currentTimeMillis(); long deltaTime = time - lastTime; lastTime = time; if (time - lastFPS > 1000) { lastFPS = time; fps = fpsCount; fpsCount = 0; } else { fpsCount++; }
update(); render(); draw(); int sleepTime = 1000/60; try { Thread.sleep(sleepTime); } catch (Exception e) { System.out.println(e); } } } setVisible(false); } |