I have a javafx project which there is a circle and four buttons(left,right,up,down). When you press a button the circle will start move on button s direction. If you press another button the circle has to change direction. If you press the circle it will turn red and the timeline will stop. The problem is every time you press the button for a second time it will update the timeline but it will make another timeline and the circle will go carzy.
I've tried to stop the timeline every time someone press a button but didn't work.
Timeline animation;
btnL.setOnAction(e -> {
if(check == false){
animation = new Timeline(new KeyFrame(Duration.millis(50), f-> moveBall('l')));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
check = true;
}
});
btnR.setOnAction(e -> {
if(check == false){
animation = new Timeline(new KeyFrame(Duration.millis(50), f-> moveBall('r')));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
check = true;
}
});
btnU.setOnAction(e -> {
if(check == false){
animation = new Timeline(new KeyFrame(Duration.millis(50), f->
moveBall('u')));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
check = true;
}
});
btnD.setOnAction(e -> {
if(check == false){
animation = new Timeline(new KeyFrame(Duration.millis(50), f-> moveBall('d')));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
check = true;
}
});
circle.setOnMouseClicked(e -> {
circle.setFill(Color.RED);
animation.stop();
check = false;
});
In the end I made a boolean check to checking if someone has press the button and the ball is moving any other button will do nothing. But I believe there is anoter solution more correct.