I am trying to figure out why I am getting two lines printed with each iteration of the while loop when I run this code:
public void run() {
long currentTime = System.nanoTime();
long targetFPS = 1000;
long delta;
while(GameConstants.running) {
delta = System.nanoTime() - currentTime;
if(delta/1000000000 >= 1) {
System.out.println(delta);
currentTime = System.nanoTime();
}
}
}
I tried to adjust the code to just call System.out.println()
once to debug, however it is still printing twice. I can't seem to figure out why this is happening since in this modified version, System.out.println(delta)
is called, then printed is set to true so System.out.println(delta)
should not be called again.
public void run() {
long currentTime = System.nanoTime();
long targetFPS = 1000;
long delta;
boolean printed = false;
while(GameConstants.running) {
delta = System.nanoTime() - currentTime;
if(delta/1000000000 >= 1 & !printed) {
System.out.println(delta);
currentTime = System.nanoTime();
printed = true;
}
}
}
Code for context:
public class Game extends Canvas implements Runnable, KeyListener {
private Thread gameThread;
public synchronized void start() {
if(GameConstants.running) return;
GameConstants.running = true;
gameThread = new Thread(this);
gameThread.start();
}
public synchronized void stop() {
if(!GameConstants.running) return;
GameConstants.running = false;
try {
gameThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
long currentTime = System.nanoTime();
long targetFPS = 1000;
long delta;
boolean printed = false;
while(GameConstants.running) {
delta = System.nanoTime() - currentTime;
if(delta/1000000000 >= 1 && !printed) {
System.out.println(delta);
currentTime = System.nanoTime();
printed = true;
}
}
}
public void update() {
if(GameConstants.gameState == 1 && !GameConstants.trialTypeDetermined ) {
Random r = new Random();
double prob = r.nextDouble();
if(prob < GameConstants.goProb) {
GameConstants.skipBad = true;
}
GameConstants.trialTypeDetermined = true;
System.out.println(GameConstants.skipBad);
}
taskTimer(GameConstants.goTaskTimes.get(getGameState(GameConstants.gameState)));
System.out.println(GameConstants.gameState);
try {
Thread.sleep(1);
} catch(InterruptedException e) {
}
}
public void render() {
}
//
public String getGameState(int gameState){
return GameConstants.gameStates[GameConstants.gameState];
}
private void skipBad() {
Random r = new Random();
double prob = r.nextDouble();
if(prob < GameConstants.goProb) {
GameConstants.skipBad = true;
}
GameConstants.trialTypeDetermined = true;
}
public void taskTimer(long taskTime) {
if(System.currentTimeMillis() - GameConstants.startTime >= taskTime) {
GameConstants.taskComplete = true;
GameConstants.startTime = System.currentTimeMillis();
nextGameState();
GameConstants.keyPressed = false;
}
}
private void nextGameState() {
if(GameConstants.gameState == 2 & GameConstants.skipBad) {
GameConstants.gameState = GameConstants.gameState + 2;
} else if(GameConstants.gameState != 5) {
GameConstants.gameState++;
} else {
GameConstants.gameState = 1;
}
GameConstants.gameStateRegistered = false;
}
private void resetParameters() {
}
private void checkHit() {
}
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_SPACE && !GameConstants.keyPressed){
GameConstants.keyPressed = true;
if(!GameConstants.reached){
GameConstants.reached = true;
GameConstants.RT = System.currentTimeMillis();
}
}
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
public static void main(String[] args) {
Game game = new Game();
game.setPreferredSize(new Dimension(GameConstants.WIDTH * GameConstants.SCALE, GameConstants.HEIGHT * GameConstants.SCALE));
game.setMaximumSize(new Dimension(GameConstants.WIDTH * GameConstants.SCALE, GameConstants.HEIGHT * GameConstants.SCALE));
game.setMinimumSize(new Dimension(GameConstants.WIDTH * GameConstants.SCALE, GameConstants.HEIGHT * GameConstants.SCALE));
JFrame frame = new JFrame("Game");
frame.setSize(GameConstants.WIDTH * GameConstants.SCALE, GameConstants.HEIGHT * GameConstants.SCALE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(game);
frame.setVisible(true);
game.start();
game.run();
}
}