Game Development Reference
In-Depth Information
images. The answer is you don't have to. Because all these images were saved as full-
screen images, TexturePacker stores the image offsets, and cocos2d then uses these off-
sets when rendering the sprites. All you really have to do is center each of these images
on the screen, and they will be at the correct place.
Look at the code for the ParallaxBackground node newly added to the
ScrollingWithJoy01 project. The header file is pretty straightforward:
@interface ParallaxBackground : CCNode
CCSpriteBatchNode* spriteBatch;
int numSprites;
I only kept a reference to the CCSpriteBatchNode around because I'll be access-
ing it in the code frequently. Storing a node as an instance variable is faster than asking
cocos2d for the node via the getNodeByTag method, and more so the more children
the node has. Keeping a reference to a node by storing it as an instance variable saves
you a few CPU cycles. It's nothing too dramatic, and it's certainly not worth keeping
several hundreds of member variables around. But it's very convenient in cases where
you need to access a particular node frequently.
In the init method of the ParallaxBackground class, the CCS-
priteBatchNode is created, and all seven background images are added from the
texture atlas, as shown in Listing 7-1 .
Listing 7-1. Loading the Background Images
-(id) init
if ((self = [super init]))
CGSize screenSize = [CCDirector sharedDirector].winSize;
// Get the game's texture atlas texture by adding it to the cache
CCTexture2D* gameArtTexture = [[CCTextureCache sharedTextureCache] ←
Search Nedrilad ::

Custom Search