The following Game loop doesn’t seem to work, as the print commands both only output 0
public static void run() {
int frames = 0;
long timePerFrameInNano = 1000000000/TARGETFPS; //TARGETFPS is set to 60
long lastUpdate = System.nanoTime();
long timer = System.nanoTime();
while(true) {
if(System.nanoTime()-lastUpdate >= timePerFrameInNano) { //If enough time passed
frames++; // count frames to check FPS after each second
lastUpdate = System.nanoTime();
update();
render();
System.out.println(frames);
}
if(timer >= 1000000000) { //After 1s, update the FPS
timer = System.nanoTime(); //Reset 1s timer
fps = frames; //All the frames counted afetr 1s should show me the current fps
System.out.println(fps);
frames = 0;
}
}
}
Instead of the expected 60FPS it only shows me 0. I’ve been going back and forth in debug mode and couldn’t figure out what’s wrong.
New contributor
Slimy Hoffy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2