My goal is getting the ball to be centered at the paddle even if the value of the ball radius were to change in future versions of the game. My only problem is implementing the correct math formula for the x coordinate of the gameball. I got the y coordinate formula working perfectly.
I do not need the correct answer. I just need guidance and suggestions to get the answer.
Here is a picture of the java program:
http://i33.photobucket.com/albums/d86/warnexus/ball.png
You can find the code below the comment "// Trouble figuring the math here".
/** Radius of the ball in pixels */
private static final int BALL_RADIUS = 500;
private void setup_Paddle()
{
// TODO Auto-generated method stub
// x coordinate of the upper left corner
// y coordinate of the upper left corner
paddle = new GRect(20,20,PADDLE_WIDTH,PADDLE_HEIGHT);
paddle.setFilled(true);
paddle.setColor(Color.PINK);
add(paddle,paddleInitialLocationX,paddleInitialLocationY);
}
private void setup_Ball()
{
// Trouble figuring the math here
int ballSetUpCoordX = (int) (paddle.getX());
// Good Code!
int ballSetUpCoordY = (int) (paddle.getY()-BALL_RADIUS);
gameBall = new GOval(BALL_RADIUS,BALL_RADIUS);
gameBall.setFilled(true);
gameBall.setColor(Color.BLUE);
add(gameBall,ballSetUpCoordX,ballSetUpCoordY);
}
private GOval gameBall;
private GRect paddle;
private int paddleInitialLocationX = 200;
private int paddleInitialLocationY = 500;