Up Next
Go up to Bouncing
Go forward to The Other Walls

The Right Wall

So, since your ball is disappearing off the right side, you should make it bounce their first. To do this, place an if statement after the assignments in "animate" but before the"fillOval" to draw the ball.

The if statement you add should check to see if the x coordinate is greater than 200 (the default width of the applet's screen area). If it is, you simply need to reverse the direction in which the ball moves horizontally. This can be done by replacing the current speed with -1 times the current speed. That is:

xspeed = -1 * xspeed;
Depending on how close to reality you want your animation to be, you could change the x value in this case to reflect the fact that the ball should have bounced instead of briefly passing through the side wall. If you don't bother, only those with good eyes will ever notice.

Once you have added the "if", run the applet. The ball should bounce off the right side and then slide off the bottom. If not, take a close look at your code and/or ask for help.


Up Next