JavaFX中的对象交互

时间:2017-04-13 01:25:52

标签: java animation javafx nodes

所以,我正在尝试编写一个程序,用一个球拍进行简单的乒乓球比赛。我现在处于两个阶段,一个用于球,一个用于桨。有两个问题,具体取决于使用的窗格类型。如果我将对象添加到堆叠窗格中,球会反弹,但我无法让挡板移动。如果我将它们添加到组中,则球拍将移动,但球不会反弹。

基本上,我在这里做错了什么,我怎样才能使两个物体都正常工作(球弹跳和桨移动)?

申请表(希望评论有帮助):

 public class JavaFXPongNW extends Application {
    @Override
    public void start(Stage primaryStage) {

        Group gameArea = new Group();           /*Paddle moves, ball does not bounce*/
        //StackPane gameArea = new StackPane(); /*Ball bounces, paddle does not move*/

        //Create a ball and paddle
        PongBall pongBall = new PongBall();
        PongPaddle pongPaddle = new PongPaddle();
        /*A regular rectangle can also be used instead of the 
        PongPaddle class, with similar results*/

        //Future buttons for pausing and playing the game
        //Button pause = new Button("Pause");
        //Button play = new Button("Play");

        //Add ball and paddle to the game
        gameArea.getChildren().addAll(pongPaddle, pongBall);

        //Push up or down arrow key to move paddle (only works in certain panes, or in a Group)
        pongPaddle.setOnKeyPressed(e -> {
            switch (e.getCode()) {
                case DOWN: pongPaddle.setLayoutY(pongPaddle.getLayoutY() + 5); break;
                case UP: pongPaddle.setLayoutY(pongPaddle.getLayoutY() - 5); break;
            }
        });

        Scene scene = new Scene(gameArea, 700, 400);
        primaryStage.setTitle("Pong");
        primaryStage.setScene(scene);
        primaryStage.show();

        pongPaddle.requestFocus();
    }
}

PongBall(这是我教科书中的代码,只有该类已被重命名):

    //The class for the ball
    class PongBall extends Pane {
    public final double radius = 15;
    private double x = radius, y = radius;
    private double dx = 1, dy = 1;
    private Circle circle = new Circle(x, y, radius);
    private Timeline animation;

    public PongBall() {
        circle.setFill(Color.BLUE);
        getChildren().add(circle);

        animation = new Timeline(
            new KeyFrame(Duration.millis(20), e -> moveBall()));
            animation.setCycleCount(Timeline.INDEFINITE);
            animation.play();
    }

    public void play() {
        animation.play();
    }

    public void pause() {
        animation.pause();
    }

    public DoubleProperty rateProperty() {
        return animation.rateProperty();
    }

    protected void moveBall() {
        if (x < radius || x > getWidth() - radius) {
            dx *= -1;
        }
        if (y < radius || y > getHeight() - radius) {
            dy *= -1;
        }

        x += dx;
        y += dy;
        circle.setCenterX(x);
        circle.setCenterY(y);
    }
}

The Paddle:

 //Class for the paddle
    class PongPaddle extends Pane {
    private Rectangle rectangle = new Rectangle(675, 50, 15, 150);

    public PongPaddle() {
        rectangle.setFill(Color.BLACK);
        getChildren().add(rectangle);
    }
}

我知道很多,而且我仍在寻找如何使用这些东西的答案。这是一个班级,但正如你所看到的,我有一个很好的开始(我认为)。我想也许我只是不知道要搜索的单词的正确组合以便找到答案。这些事情会变得相当复杂。

我找到了这个例子:How to make the ball bounce off the walls in JavaFX?

和这一个:How to make ball bounce off an object in Javafx?

所以我会测试这些是为了看看哪些有效,希望我可以自己解决这个问题,就像我的上一个问题一样。

0 个答案:

没有答案