Game Development Reference
In-Depth Information
Adding Parallax to the Cloud Object
1.
Reopen
obj_clouds
and edit the
Set Variable
action that sets
parallax_y
to
0
and set it
to
-view_top/2
instead. This parallax value gets subtracted from the vertical drawing
position of the cloud sprites so it will draw them further up, as the view itself moves
further down in the room. This is what you would expect: as your view moves up, the
thing you are looking at moves down with respect to that view. However, as the value is
divided by two, the clouds move up and down at half the speed of the view, giving the
impression that they are further away than the foreground objects.
2.
Edit the first
Draw Rectangle
action and set
Y1
to
y+parallax_y
, as we now need to
take the parallax into account too.
3.
Edit the
Set Variable
action that sets
parallax_x
to
0
(at the start of the repeated
block)
and set it to
-view_left/(cloud_index+2)
instead. This parallax value gets subtracted
from the horizontal drawing position of the cloud sprites so it will draw them further
toward the left, as the view itself moves further right in the room. This time, the value is
divided by
cloud_index+2
, where
cloud_index
starts at
0
and increases by one for each
cloud layer. So the first layer of clouds moves horizontally at 1/2 the speed of the view,
but the second moves at 1/3 the speed, the third at one 1/4, and so on. In this way, we
get multiple levels of horizontal parallax that move less, and thus appear to be further
away as the clouds get higher up the screen.
4.
Edit the final
Draw Rectangle
action and set
Y2
to
y+parallax_y+32
.
If you play the game now, then things aren't quite there yet. However, you can clearly see
these multiple levels of parallax in action, as the cloud layers separate like a staircase as you move
across the level because of their differing movement speeds. Of course, this isn't what we want,
and there is one last problem to solve. Our cloud sprites are only 512 pixels long, but so is our
view so we're going to need to repeat the sprite in order to make it look like the clouds are
continuous. However, even with two lengths of cloud, we're going to eventually reach the end of
them, so we also need to get them to wrap around. In other words, once the length of cloud
disappears off one side of the view, it needs to jump back to the other side of the view ready to
come on again. That way, the clouds can continue to scroll indefinitely.
Adding Parallax to the Cloud Object
1.
Reopen
obj_clouds
. Directly before the
Draw Sprite
action, include a
Set Variable
action (
control
tab) that sets
parallax_x
to
parallax_x
mod
512
. Recall that the
mod
operator divides the left value by the right value, and then provides the remainder, so it
will always produce a number between 0 and 511. As soon as it reaches 512, it will
return to 0 and start again, producing our wrapping effect for us.
2.
Follow this with another
Draw Sprite
action (
draw
tab) that draws the
spr_clouds
Sprite
again, but this time at a
Relative
position of
X
=
parallax_x+512
,
Y
=
parallax_y
with the
Subimage
cloud_index
. This is just directly to the right of the other sprite.
And that's it— you should now have a parallax scrolling background that significantly
improves the look of the game. If there is any problem with your version, then load the file
zool14.gmk
from the
Chapter06/Games
directory on the CD. If you're still a bit confused as to what
is actually going on to achieve this effect, then take a look at
zool14a.gmk
on the CD as well. This
version is set up to allow you to see much more of the room as Zool moves about the level and
you can see the wrapping effect of the clouds in action. It looks very strange in this view, but the
player is usually completely oblivious to what is going on to achieve this effect.
Search WWH ::
Custom Search