I am trying to bounce a ball image between the boundaries of my window. However when the ball gets to the top boundary it does not bounce back. I hope some can help me solve this issue.
Here is my code:
import java.awt.*;
import java.applet.*;
public class Ball {
private int x=355 ;
private int y=500;
private int xVel = -3;
private int yVel = 3;
private Image ball;
public Ball (Breakout bR){
ball = bR.getImage(bR.getDocumentBase(),"ball.png");
}
public void update(Breakout bR){
x += xVel;
y += yVel;
if (x < 0){
xVel = 3;
}
else if (x > bR.getWidth()){
xVel = -3;
}
if(y > bR.getHeight()){
yVel = -3;
}
else if (y < 0){
xVel = 3;
}
}
public void paint (Graphics g, Breakout bR){
g.drawImage(ball,x,y,bR);
}
}
Thanks for your help :)
You have a typo in your last else-if... It should read
else if (y < 0){
yVel = 3;
}