0

Why doesnt the ball move 10 then wait 1000ms then repeat 10 times

Hi I need help with my Java FX application. I want the ball to move 10 then wait then move again and repeat. I have created my button, circle and anchorpane in Scene Builder. The ball always only move at the end of the for loop please help. Here is my code public class Main extends Application{ public static void main(String [] args){ launch(args); } @Override public void start ( Stage stage ) throws Exception { Parent loginFOrm = FXMLLoader.load (getClass().getResource ("Resources/Bounce.fxml")); Scene loginform = new Scene(loginFOrm); stage.setScene (loginform); stage.show (); } } public class Bouncecontroller { @FXML Button start; @FXML Circle ball; boolean gameRunning; double ballX, ballY = 0; public void startGame ( MouseEvent mouseEvent ) throws InterruptedException { start.setVisible (false); gameRunning = true; for (int i = 0; i < 10; i++) { ballX = ball.getCenterX () + 10; System.out.println (ballX); sleep(1000); ball.setTranslateX (ballX); } } }

5th May 2020, 3:34 PM
Stephen
2 Réponses
+ 2
Hello Stephen For animations you can't use a loop. You will need to use TimeLine or PathTransition. Here are some tutorials about animations: https://docs.oracle.com/javafx/2/animations/basics.htm zetcode.com/gui/javafx/animation/ If you need help, just ping me :)
6th May 2020, 12:41 AM
Denise Roßberg
Denise Roßberg - avatar
+ 1
ok thanks 👍
6th May 2020, 4:34 AM
Stephen