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.