Game Development Reference
In-Depth Information
[speedFactors addObject:[NSNumber numberWithFloat:1.2f]];
[speedFactors addObject:[NSNumber numberWithFloat:1.2f]];
NSAssert(speedFactors.count == numStripes,
@"speedFactors count does not match numStripes!");
scrollSpeed = 1.0f;
[self scheduleUpdate];
The final assert is simply a safety check for human error. Consider that you may be
adding or removing stripes from the background for whatever reason but might forget
to adjust the number of values you're adding to the speedFactors array. If you for-
get to modify the speedFactors initialization, the assert will remind you of it, in-
stead of potentially crashing the game seemingly at random at a later time.
In Figure 7-5 you can see which speed factor is applied to which stripe. Stripes with
higher speed factors move faster than those with slower ones, which creates the paral-
lax effect. Once again, I use dummy graphics to make each individual stripe clearly
Figure 7-5 . The speed factors applied to each background stripe
To use the newly introduced speed factors, change the update method to this:
-(void) update:(ccTime)delta
for (CCSprite* sprite in spriteBatch.children)
NSNumber* factor = [speedFactors objectAtIndex:sprite.zOrder];
Search Nedrilad ::

Custom Search