I am new to Javafx and I am creating a simple program. What I'm trying to achieve is get the ball to bounce off the walls, but I haven't figured out how to do that yet. Also, feel free to leave other suggestions about my code.
Here's the source code:
public class GamePractice extends Application {
public static Circle circle;
public static Pane canvas;
private long counter = 0;
@Override
public void start(Stage primaryStage) {
canvas = new Pane();
Scene scene = new Scene(canvas, 800, 600);
primaryStage.setTitle("Game");
primaryStage.setScene(scene);
primaryStage.show();
circle = new Circle(15,Color.BLUE);
circle.relocate(100, 100);
canvas.getChildren().addAll(circle);
Timeline loop = new Timeline(new KeyFrame(Duration.millis(10), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
if (counter++ % 10 == 0)
{
circle.setLayoutX(circle.getLayoutX() + 10);
circle.setLayoutY(circle.getLayoutY() + 10);
//This is what I currently have, am I headed the right direction?
//Check X and Y for collision with the ball
if ((circle.getTranslateX() == canvas.getWidth() - 10) || (circle.getTranslateY() == canvas.getHeight() - 10)) {
//How do I make the ball bounce?
}
}
}
}));
loop.setCycleCount(Timeline.INDEFINITE);
loop.play();
}
I appreciate the help.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…