vendredi 1 février 2019

My if statement that displays my ticks and frames is executing twice

I have a simple if statement here that just prints out the frames and ticks I'm getting for my game.

if(System.currentTimeMillis() - lastTimer >= 1000) {
                lastTimer += 1000;
                System.out.println("fps: " + frames + ", ticks: " + ticks);
                frames = 0;
                ticks = 0;
            }

For some reason when running the code I'm getting two lines of the same thing being printed at a time for example:

fps: 120, ticks: 109
fps: 120, ticks: 109
fps: 103, ticks: 112
fps: 103, ticks: 112

I'm not sure what is causing this issue and this has been a problem for me with other code as well where a single statement is being executed twice on the spot. Is there any fix for this issue?

UPDATE:

Here is some code surrounding the if statement

while(running) {
            long now = System.nanoTime();
            delta += (now - lastTime) / nsPerTick;
            lastTime = now;
            boolean shouldRender = true;
            while(delta >= 1) {
                tick();
                render();
                delta -= 1;
                shouldRender = true;            
            }

            if(System.currentTimeMillis() - lastTimer >= 1000) {
                lastTimer += 1000;
                System.out.println("fps: " + frames + ", ticks: " + ticks);
                frames = 0;
                ticks = 0;
            }
        }

Aucun commentaire:

Enregistrer un commentaire