1

So, I am trying to write a program that will play a simple game of pong, with one paddle. I am at a point now where I have two classes, one for the ball, and one for the paddle. There are two problems, depending on what type of pane is used. If I add the objects to a stackpane, the ball will bounce but I can't get the paddle to move. If I add them into a group, the paddle will move, but the ball does not bounce.

Basically, what am I doing wrong here, and how can I get both objects to work properly(ball bouncing and paddle moving)?

The application(hopefully the comments are helpful):

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

The PongBall (this is code from my textbook, only the class has been renamed):

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

I know it's a lot, and I'm still looking for answers in how to work with this stuff. It is for a class, but as you can see I've got a pretty good start on it (I think). I think maybe I just don't know the proper combination of words to search for in order to find the answers. These things can get pretty complicated.

I have found this example: How to make the ball bounce off the walls in JavaFX?

and this one: How to make ball bounce off an object in Javafx?

So I will be testing these out to see what works, hopefully I can figure this one out on my own, like my last question.

Community
  • 1
  • 1
  • tl;dr but try using a `Pane` instead of either a `StackPane` (which by default centers its child nodes) or a `Group` (which adjusts its own bounds to contain the bounds of its child nodes). – James_D Apr 13 '17 at 01:45
  • I think that a Group is the right path, but as of right now when I use a Group the ball just falls through the bottom of the window. In a StackPane, it will bounce properly, but the paddle won't move. It draws on the right side of the frame, as intended, but that's it. – Nicholas Willette Apr 13 '17 at 02:20
  • I think the dimensions of the `PongBall` will expand to contain the ball (at least if it is contained in a group). So your conditions for bouncing will never be met. You probably need to set the limits for bouncing from the container in which `PongBall` is placed (you could use a binding if needed). I still think a `Group` is wrong here, but I think the pane growing is the cause of the problem: you could log the height of `PongBall` to check if this is the case. – James_D Apr 13 '17 at 02:31
  • I had not thought of that, thank you. I will look into it. To be honest, I'm pretty new at this, so I had assumed the container would basically take on the size of the object it was containing, and then stay that way. – Nicholas Willette Apr 13 '17 at 02:35
  • Well, it takes on the size of the object(s) it contains, but if they move it will, if possible, grow to accommodate them. – James_D Apr 13 '17 at 02:40
  • Oh, I see what you mean. Ok, I think I can figure that out. Thanks. – Nicholas Willette Apr 13 '17 at 21:13

0 Answers0