refactoring this function in Java
- by Joel
Hi folks,
I'm learning Java, and I know one of the big complaints about newbie programmers is that we make really long and involved methods that should be broken into several. Well here is one I wrote and is a perfect example. :-D.
public void buildBall(){
/* sets the x and y value for the center of the canvas */
double i = ((getWidth() / 2));
double j = ((getHeight() / 2));
/* randomizes the start speed of the ball */
vy = 3.0;
vx = rgen.nextDouble(1.0, 3.0);
if (rgen.nextBoolean(.05)) vx = -vx;
/* creates the ball */
GOval ball = new GOval(i,j,(2 *BALL_RADIUS),(2 * BALL_RADIUS));
ball.setFilled(true);
ball.setFillColor(Color.RED);
add(ball);
/* animates the ball */
while(true){
i = (i + (vx* 2));
j = (j + (vy* 2));
if (i > APPLICATION_WIDTH-(2 * BALL_RADIUS)){
vx = -vx;
}
if (j > APPLICATION_HEIGHT-(2 * BALL_RADIUS)){
vy = -vy;
}
if (i < 0){
vx = -vx;
}
if (j < 0){
vy = -vy;
}
ball.move(vx + vx, vy + vy);
pause(10);
/* checks the edges of the ball to see if it hits an object */
colider = getElementAt(i, j);
if (colider == null){
colider = getElementAt(i + (2*BALL_RADIUS), j);
}
if (colider == null){
colider = getElementAt(i + (2*BALL_RADIUS), j + (2*BALL_RADIUS));
}
if (colider == null){
colider = getElementAt(i, j + (2*BALL_RADIUS));
}
/* If the ball hits an object it reverses direction */
if (colider != null){
vy = -vy;
/* removes bricks when hit but not the paddle */
if (j < (getHeight() -(PADDLE_Y_OFFSET + PADDLE_HEIGHT))){
remove(colider);
}
}
}
You can see from the title of the method that I started with good intentions of "building the ball".
There are a few issues I ran up against:
The problem is that then I needed to move the ball, so I created that while loop. I don't see any other way to do that other than just keep it "true", so that means any other code I create below this loop won't happen. I didn't make the while loop a different function because I was using those variables i and j.
So I don't see how I can refactor beyond this loop.
So my main question is:
How would I pass the values of i and j to a new method: "animateBall" and how would I use
ball.move(vx + vx, vy + vy); in that new method if ball has been declared in the buildBall method?
I understand this is probably a simple thing of better understanding variable scope and passing arguments, but I'm not quite there yet...