Out of bounds!
If you run our current game, you will notice that the robot will go off the screen if you allow him to continue moving to the left or right. When he reaches the edge of the screen, he will keep on moving until he is no longer visible. If you reverse his direction and make him move the same number of steps now, he will reappear on the screen.
Whenever an object reaches the edge of the screen, we often want it to do something special, such as stopping, or turning around. The code that determines when an object has reached a screen edge is known as boundary checking. There are many possibilities for what we can do when an object reaches a boundary:
- Stop the object
- Allow the object to continue past the border (and therefore, disappear)
- Allow the object to continue past the border and reappear at the opposite border (ever played the arcade version of Asteroids?)
- Scroll the camera and the screen along with the object (aka Mario)
- Allow the object to rebound off the border (ever played...