Reputation: 333
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..
Upvotes: 0
Views: 1831
Reputation: 1988
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.
P.s. I can upload the fixed version to Git if you want.
Upvotes: 2