Game Development Reference
In-Depth Information
CGPoint pos = sprite.position;
pos.x - = (scrollSpeed * factor.floatValue) * (delta * 50);
sprite.position = pos;
Based on the sprite's zOrder property, a speed factor NSNumber is obtained from
the speedFactors array. This is multiplied by the scrollSpeed to speed up or
slow down the movement of individual stripes. You can't multiply the NSNumber ob-
ject directly because it's a wrapper class storing a primitive data type such as float ,
int , char , or others. NSNumber has a floatValue property that returns the
floating-point value stored in it, and it has a number of other properties to retrieve dif-
ferent data types as well. You could also use intValue , even though this NSNumber
stores a floating-point value. It's essentially the same as casting a float to an int .
Once again, the delta time is also factored in to make the scrolling speed independ-
ent of the framerate.
By using the speedFactors array and giving the same-colored stripes the same
factor, the background stripes will now move as expected. But there's still the issue of
making endless scrolling.
Scrolling to Infinity and Beyond
Also in ScrollingWithJoy02 is the first step toward endless scrolling. You'll find in
Listing 7-4 , which repeats the snippet of code first introduced in Listing 7-3 , seven
more background stripes to the CCSpriteBatchNode , although with a slightly dif-
ferent setup.
Listing 7-4. Adding Off-Screen Background Images
// Add seven more stripes, flip them, and position them next to their neighbor stripe
for (int i = 0; i < numStripes; i++)
NSString* frameName = [NSString stringWithFormat:@"bg%i.png", i];
CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:frameName];
Search Nedrilad ::

Custom Search