Game Development Reference
In-Depth Information
Every background image's x position gets subtracted a bit every frame to scroll it from
right to left. How much an image moves depends on the predefined scrollSpeed
plus the sprite's zOrder . The delta multiplier is used to make the scrolling speed inde-
pendent of the framerate, which is then multiplied by 20 to make the scrolling reason-
ably fast. Images that are closer to the screen scroll faster. However, using the zOrder
property causes the stripes that should be at the same visual depth to scroll at different
The position is also multiplied by the delta time to make the scrolling speed independ-
ent of the framerate. The delta time itself is just a tiny fraction—it's the time between
two calls to the update method. At exactly 60 frames per second (FPS), it's 1/60 of a
second, which is a delta time of 0.167 seconds. For that reason, I multiply delta by 20
just to get a reasonably fast scroll; otherwise the images move too slowly.
Parallax Speed Factors
Somehow the stripes of the same color need to scroll at the same speed, and the stripes
should repeat so that the background doesn't show up. My solutions to these issues are
in the ScrollingWithJoy02 project.
The first change has to do with scrolling speed. I decided to use a NSMutableArray
to store the speed factor with which individual stripes move. Other solutions are avail-
able, but this allows me to illustrate a key issue of NSMutableArray and, in fact, all
iOS SDK collection classes: they can store only objects, never values such as integers
and floating-point numbers.
The way around this is to box numbers into an NSNumber object. The following code
is the newly added NSMutableArray* speedFactors , which stores floating-
point values. The array is defined in the ParallaxBackground class header:
@interface ParallaxBackground : CCNode
CCSpriteBatchNode* spriteBatch;
int numStripes;
NSMutableArray* speedFactors;
float scrollSpeed;
Search Nedrilad ::

Custom Search