The problem I am having is that every time I press the "Single Player" button, which starts the game of Snake, a new instance of the GameTimer class initializes, the problem is that when I click the "Back" button the stopTimer method doesn't manage to cancel or stop the timer and it just ignores it. The timer features works well, but it's highly ineffective and simply said stupid.
Problem: stopTimer() method doesn't cancel the timer.
Here is my project on github: https://github.com/AquaSolid/JavaFX_Snake/blob/master/src/Snake/GameTimer.java
...code omitted..
public void stopTimer() {
timerTask.cancel();
timer.cancel();
timer.purge();
isActive = false;
} ...code omitted..
That bit of your code works perfectly. However:
This will produce NullPointerException in some cases:
Stage stage = (Stage) buttonBackToMenu.getScene().getWindow();
This will work:
Node target = (Node) event.getTarget();
Stage stage = (Stage) target.getScene().getWindow();
Good luck.
Ps I can upload the fixed version to Git if you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.