0

I'm currently working on this Clash Royale style game in Java FX and I have a game timer in one class that I set in my test condition. However, when I check its value over on the GameView class it's always false. I've looked at many examples and the latest I tried making my properties static in my timer class but that didn't have any effect either. Hoping someone could steel me in the right direction. Thanks!

public class GameTimer  {
public int seconds = 180;
public static boolean endGame;


 public static boolean returnBool () {
    return endGame;
}

public static boolean isEndGame() {
    return endGame;
}

public static void setEndGame(boolean endGame) {
    GameTimer.endGame = endGame;
}
Timer timer = new Timer();
TimerTask task = new TimerTask(){
    public void run(){
        seconds--;
        System.out.println("180 / " +  seconds);

       if (seconds == 170){
         System.out.println("170 seconds hit the spot" );
         endGame = true;
         this.cancel();
        }

    } 
};

public void start(){
    timer.scheduleAtFixedRate(task, 1000, 1000);
    isEndGame();
    }   
}

public class GameViewManager {

public void gameTimer() {
     GameTimer timer = new GameTimer();
         timer.start();
     //System.out.println(timer.returnBool());

        if (timer.isEndGame()){
        //gameStage.close();
        System.out.println("test");
       }
      }
    }
guyboris
  • 27
  • 3
  • 1
    Via `timer.start()` you're scheduling the task to be executed regularly. The first time the task is run is specified to be 1 second after the method is run and it needs to be executed 10 times to take effect. It is almost impossible for your program to take 10 second to get from `timer.start()` to `if (timer.isEndGame())`... Also you're not properly synchronizing the data. You may never see the update. I strongly recommend considering the `Timeline` approach presented here: https://stackoverflow.com/questions/9966136/javafx-periodic-background-task – fabian Nov 24 '19 at 12:37
  • unrelated: static access is very very rarely what you want .. – kleopatra Nov 24 '19 at 12:47

0 Answers0