Comment faire rebondir la balle sur les murs dans JavaFX?


Je suis nouveau sur Javafx et je crée un programme simple. Ce que j'essaie de réaliser, c'est de faire rebondir la balle sur les murs, mais je n'ai pas encore compris comment le faire. Aussi, n'hésitez pas à laisser d'autres suggestions sur mon code.

Voici le code source:

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();
    }

J'apprécie l'aide.

Author: Anshul Parashar, 2013-11-16

1 answers

Un indice: vous devriez éviter de comparer des valeurs doubles pour l'égalité exacte a == b.

Avec quelques petits changements de votre code, vous êtes déjà là:

package learn.javafx;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class GamePractice extends Application {

    public static Circle circle;
    public static Pane canvas;

    @Override
    public void start(final Stage primaryStage) {

        canvas = new Pane();
        final 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);

        final Timeline loop = new Timeline(new KeyFrame(Duration.millis(10), new EventHandler<ActionEvent>() {

            double deltaX = 3;
            double deltaY = 3;

            @Override
            public void handle(final ActionEvent t) {
                circle.setLayoutX(circle.getLayoutX() + deltaX);
                circle.setLayoutY(circle.getLayoutY() + deltaY);

                final Bounds bounds = canvas.getBoundsInLocal();
                final boolean atRightBorder = circle.getLayoutX() >= (bounds.getMaxX() - circle.getRadius());
                final boolean atLeftBorder = circle.getLayoutX() <= (bounds.getMinX() + circle.getRadius());
                final boolean atBottomBorder = circle.getLayoutY() >= (bounds.getMaxY() - circle.getRadius());
                final boolean atTopBorder = circle.getLayoutY() <= (bounds.getMinY() + circle.getRadius());

                if (atRightBorder || atLeftBorder) {
                    deltaX *= -1;
                }
                if (atBottomBorder || atTopBorder) {
                    deltaY *= -1;
                }
            }
        }));

        loop.setCycleCount(Timeline.INDEFINITE);
        loop.play();
    }

    public static void main(final String[] args) {
        launch(args);
    }
}
 6
Author: Jens Piegsa, 2017-05-23 11:53:55