Game Development Reference
In-Depth Information
// Create the background spritebatch
spriteBatch = [CCSpriteBatchNode batchNodeWithTexture:gameArtTexture];
[self addChild:spriteBatch];
// Add the 6 different layer objects and position them on the screen
for (int i = 0; i < 7; i++)
{
NSString* frameName = [NSString stringWithFormat:@"bg%i.png", i];
CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:frameName];
sprite.position = CGPointMake(screenSize.width / 2, ←
screenSize.height / 2);
[spriteBatch addChild:sprite z:i];
}
scrollSpeed = 1.0f;
[self scheduleUpdate];
}
return self;
}
First you're getting the game-art.pvr.czz texture from the CCTextureCache .
Normally you'd use the textureForKey method to access the already loaded tex-
ture. But if you're not sure whether the texture is already in the cache, you can also use
the addImage method instead of textureForKey . That also loads the texture if it
wasn't cached—otherwise addImage just returns the cached texture. As long as the
image is included in the app bundle, addImage is guaranteed to return a valid texture.
With the CCSpriteBatchNode created and set up, the next step is to load the seven
individual background images. I deliberately chose to number them from 0 to 6 so you
can use stringWithFormat to create the filenames as strings in a very effective
way:
NSString* frameName = [NSString stringWithFormat:@"bg%i.png", i];
With that sprite frameName , create a CCSprite as usual and then position it at the
center of the screen:
Search Nedrilad ::




Custom Search