Class 布尔值不将值从一个类更改为另一个类
我目前正在用JavaFX开发这款Clash Royale风格的游戏,我在测试条件中设置了一个类中的游戏计时器。但是,当我在GameView类上检查它的值时,它总是为false。 我看了很多例子,最近我尝试在timer类中使属性保持静态,但也没有任何效果。希望有人能帮我找到正确的方向。谢谢Class 布尔值不将值从一个类更改为另一个类,class,javafx,Class,Javafx,我目前正在用JavaFX开发这款Clash Royale风格的游戏,我在测试条件中设置了一个类中的游戏计时器。但是,当我在GameView类上检查它的值时,它总是为false。 我看了很多例子,最近我尝试在timer类中使属性保持静态,但也没有任何效果。希望有人能帮我找到正确的方向。谢谢 public class GameTimer { public int seconds = 180; public static boolean endGame; public static boole
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");
}
}
}
通过
timer.start()。任务第一次运行的时间指定为方法运行后1秒,需要执行10次才能生效。您的程序几乎不可能在10秒钟内从timer.start()
到if(timer.isEndGame())
。。。此外,您没有正确同步数据。您可能永远看不到更新。我强烈建议考虑这里介绍的时间线
方法:无关:静态访问很少是您想要的。。