Game Development Reference
In-Depth Information
Note
In game programming, the layers of objects are made by the sequence they are
added in. So for the preceding example, the Spaceship should be added after
SpaceBackground giving an appearance that the ship is above the back-
ground.
The sequence of the views presented on the screen can be changed by altering their z co-
ordinate; the view having highest z coordinate will always be on the top. This means we
can explicitly define which layer we want to keep on top and which we want to keep at
the bottom, which is explained in the following steps:
1. The initial background is added but it's not scrolling. This could be accomplished
by the update method discussed in the Anatomy of game projects recipe.
2. For this, some math is required to implement this functionality. Build some inline
functions and constants to be used for infinitely moving the background. This is
the code needed:
static const float SPACE_BG_VELOCITY = 100.0;
static inline CGPoint CGPointAdd(const CGPoint a,
const CGPoint b)
{
return CGPointMake(a.x + b.x, a.y + b.y);
}
static inline CGPoint CGPointMultiplyScalar(const
CGPoint a, const CGFloat b)
{
return CGPointMake(a.x * b, a.y * b);
}
3. Just add these line of code preceding to the implementation of FSMyScene .
4. Now the real way to do it is, in the update method, iterate all nodes added in
FSMyScene , identify the SpaceBackground node by its name assigned in
the initialization function, and adjust its position to enable infinite scrolling. Do
all of this in a function named moveSpaceBackground .
- (void)moveSpaceBackground
{
[self enumerateChildNodesWithName:@"SpaceBG"
Search WWH ::




Custom Search