Game Development Reference
In-Depth Information
which definitely would be larger than what would fit in a single texture. Each seg-
ment can then be positioned at the appropriate x- and y-coordinates in the world
when loaded. Luckily, loading the segments at their correct positions in the world
is a quick calculation if the background sprites are drawn relative to their top-left
corner. As each background is loaded at the correct world position, it could be ad-
ded to a linked list. For example, the setup code for horizontal scrolling would be
as follows:
Click here to view code image
const int screenWidth = 960 // An iPhone 4/4S side-
ways is 960x640
// All the screen-sized image backgrounds
string backgrounds [] = { "bg1.png", "bg2.png",
/*...*/ }
// The total number of screen-sized images hori-
zontally
int hCount = 0
foreach string s in backgrounds
Sprite bgSprite
bgSprite.image .Load(s)
// 1st screen would be x=0, 2nd x=960, 3rd
x=1920, ...
bgSprite.x = hCount * screenWidth
bgSprite.y = 0
bgSpriteList .Add( bgSprite )
screenCount ++
loop
Once the bgSpriteList has been populated, we then need to determine which
backgrounds should be drawn and in what position. If the background segments
are the size of the screen, no more than two background segments can be onscreen
at once. We need a way to track what's on screen and display the appropriate back-
grounds.
One common approach is to have x- and y-coordinates for the camera also ex-
pressed as a position in the world. The camera starts out pointing at the center of
the first background image. In horizontal scrolling, the camera's x-position is set
to the player's x-position so long as the position does not scroll behind the first
background or past the last background.
Search WWH ::




Custom Search