Game Development Reference
In-Depth Information
Click here to view code image
float speedFactor = 0.2f
draw s at ( s.x - ( camera.x - screenWidth /2) *
speedFactor , 0)
For infinite scrollers, certain background layers may have less variety than others.
The cloud layer may only have ten total segments that are repeated in perpetuity,
whereas more readily apparent layers might have the aforementioned randomiza-
tion. And, of course, frameworks that already support layers work perfectly with
parallax scrolling as well.
For further detail on parallax scrolling, you should look at the sample 2D game
implemented in Chapter 13 . It implements infinite horizontal scrolling with mul-
tiple background layers in order to create the parallax effect.
Four-Way Scrolling
In four-way scrolling, the game world scrolls both horizontally and vertically, as
in most 2D Mario games since Super Mario Bros. 2 . Because it's scrolling in both
directions, up to four different background segments can be onscreen at any one
time.
In order to update the horizontal scrolling code so it works in both directions, we
will need to add code that checks whether or not the camera's y-position needs
to be updated. But first, we need to declare variables that tell us what the screen
height is and how many vertical segments tall the level is:
Click here to view code image
const int screenHeight = 640 // Height of iPhone
4/4S horizontally
int vCount = 2
One issue that comes up with four-way scrolling is placement of the origin. Be-
cause most 2D frameworks have a coordinate system where the top left of the
screen is (0,0), it simplifies things if the top left of the game world is also (0,0). So
if our game supports two vertical segments, (0,0) would be one segment above the
starting segment, as in Figure 2.8 .
Search WWH ::




Custom Search