Game Development Reference
point, the ^]_gcnkqj` will stop moving, but the lh]uan won't be able to travel all the way to
stage's right side. This seems to be an artificial constraint that would be a frustrating limitation
for the lh]uan in many action or adventure games. Figure 6-15 illustrates this problem.
When the background stops
scrolling, the player object
is unable to move all the
way to the edge of the stage.
Figure 6-15. When the background has reached the limit of its scrollable
area, the player can't travel all the way to the stage's edge.
The other potential problem is that the scrolling background always scrolls. For many games,
it might make more sense if the ^]_gcnkqj` scrolls only when the lh]uan object is approach-
ing the edge of the stage. Otherwise, the lh]uan object should explore freely without the
Here's how to solve these problems. You can set up a system that figures out whether the lh]uan
object should move or the ^]_gcnkqj` object should move, depending on where each is. If the ^]_g)
cnkqj` object has reached the limit of its movement, the lh]uan object should be free to travel to the
edge of the stage. Also, if the lh]uan is not near any of the stage's edges, it should be free to move
around without the ^]_gcnkqj` object moving.
The trick of making this work is to set up an imaginary inner boundary , which is a rectangular area
inside the real stage that's exactly half the stage's height and width. The lh]uan object will be free to
move around within the inner boundaries, and when it reaches the edge, it will stop moving, and the
^]_gcnkqj` will start to scroll. When the ^]_gcnkqj` reaches its scroll limit, the inner boundary that's
been blocking the lh]uan from going farther will extend to the limits of the real stage to allow the
lh]uan to move right to the edge.
The inner boundary that you'll create isn't a real object in the way that lh]uan and ^]_gcnkqj` are
objects. Instead, it's just four numbers that define the top, bottom, left, and right of these boundaries.
In fact, the logic behind finding these numbers is exactly the same as the logic you used to set the real
stage boundaries; you just cut them down to half the size.
You'll build this code in two stages so that you can see how it's working. (For the complete final work-
ing version of this code, see the I]ej[O_nkhhejc[Psk*]o file in this chapter's source files.)