Java Reference
In-Depth Information
/**
* Controls whether the startButton is visible
*/
BooleanProperty startVisible = new SimpleBooleanProperty(true);
/**
* The animation of the ball
*/
Timeline pongAnimation;
/**
* Controls whether the ball is moving right
*/
boolean movingRight = true;
/**
* Controls whether the ball is moving down
*/
boolean movingDown = true;
/**
* Sets the initial starting positions of the ball and paddles
*/
void initialize() {
centerX.setValue(250);
centerY.setValue(250);
leftPaddleY.setValue(235);
rightPaddleY.setValue(235);
startVisible.set(true);
pongComponents.requestFocus();
}
/**
* Checks whether or not the ball has collided with either the paddles,
* topWall, or bottomWall. If the ball hits the wall behind the paddles, the
* game is over.
*/
void checkForCollision() {
if (ball.intersects(rightWall.getBoundsInLocal())
|| ball.intersects(leftWall.getBoundsInLocal())) {
pongAnimation.stop();
initialize();
} else if (ball.intersects(bottomWall.getBoundsInLocal())
|| ball.intersects(topWall.getBoundsInLocal())) {
movingDown = !movingDown;
} else if (ball.intersects(leftPaddle.getBoundsInParent()) && !movingRight) {
movingRight = !movingRight;
} else if (ball.intersects(rightPaddle.getBoundsInParent()) && movingRight) {
movingRight = !movingRight;
}
}
Search WWH ::




Custom Search