Game Development Reference
The other bits of new code that you added in this section follow exactly the same logic for each of the
three other boundaries.
You can find the entire working example of this code in the I]ej[O_nkhhejc[Psk*]o file in the
O_nkhhejc>]_gcnkqj` folder in the chapter's source files.
Taking it further
You'll be able to get quite a bit of mileage out of these examples of scrolling for your games. Any type
of scrolling system you can dream up will use these same techniques in some kind of combination.
You've actually tackled the most difficult type of scrolling, combined vertical and horizontal scrolling,
so if you need to make a game that requires only horizontal scrolling, it should be a piece of cake. The
techniques are exactly the same; you just need half the amount of code because you'll check boundar-
ies only on the x axis.
This is not the last word on scrolling; it's really just the beginning. Have a look at some of your favorite
games and study very carefully how they've implemented scrolling. You'll notice that many of them
modify how and when scrolling takes place in very subtle ways. The core of all this, however, is based
on the examples you looked at in this chapter.
There's one additional scrolling technique that you'll look at very briefly here because it's very widely
used and extremely effective: parallax scrolling .
Parallax is a visual effect in which the position of an object appears to change depending on the point
of view from which it's being observed. The effect of parallax scrolling in games is used to create the
illusion of shallow depth. It's a simple 3D effect in which distant background objects move at a slower
rate than closer foreground objects, creating the illusion that slower-moving objects are farther away.
Parallax scrolling can give even simple 2D games very strong visual impact.
It's very to easy to do. So easy, in fact, that I won't spoil the fun you'll have by figuring it out on your
own. But let me at least give you a few hints about how to make parallax scrolling work.
First, you need at least two ^]_gcnkqj` objects (they might have the instance names bknacnkqj` and
^]_gcnkqj`). The ^]_gcnkqj` object is the one farther in the distance. Figure 6-19 shows what this
might look like in a horizontal scrolling game, in which the hilly meadow is the bknacnkqj` object, and
the sky scene is the ^]_gcnkqj` object.
Now all you need to do is move the ^]_gcnkqj` object at a slower rate than the bknacnkqj` object.
The directives might look like this:
Try it! It's a mesmerizing effect. And there's also nothing stopping you from adding a third element as
a distant background object moving at an even slower rate.