when diving into “learn cocos2d game development with ios5”, in ch08
in EnemyCache.m
-(id) init
{
if ((self = [super init]))
{
// get any image from the Texture Atlas we're using
CCSpriteFrame* frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"monster-a.png"];
batch = [CCSpriteBatchNode batchNodeWithTexture:frame.texture];
[self addChild:batch];
[self initEnemies];
[self scheduleUpdate];
}
return self;
}
so batch is with texture “monster-a.png”
in EnemyEntity.m‘s initWithType method
switch (type)
{
case EnemyTypeUFO:
enemyFrameName = @"monster-a.png";
bulletFrameName = @"shot-a.png";
break;
case EnemyTypeCruiser:
enemyFrameName = @"monster-b.png";
bulletFrameName = @"shot-b.png";
shootFrequency = 1.0f;
initialHitPoints = 3;
break;
case EnemyTypeBoss:
enemyFrameName = @"monster-c.png";
bulletFrameName = @"shot-c.png";
shootFrequency = 2.0f;
initialHitPoints = 15;
break;
default:
[NSException exceptionWithName:@"EnemyEntity Exception" reason:@"unhandled enemy type" userInfo:nil];
}
if ((self = [super initWithSpriteFrameName:enemyFrameName]))
{
//...
}
so the returned object may be in 3 different frame. since Only the CCSprites that are contained in that texture can be added to the CCSpriteBatchNode, obviously, ‘monster-b.png’ is not contained in ‘monster-a.png’, why the different enemy can still be added to the batch?
It is possible to place multiple images in to one. Such image is called an atlas. So all the monsters are in one big texture. The access to each image is done using .plist configuration filed where the information about where concrete image is placed in the big texture is stored.
It has a lot of sense because it’s an expensive operation to swtich textures. Much faster is to place everything you need in to one texture and use batching. Also it also takes less memory on the GPU since textures are stored in (2^n, 2^m) buffers on the GPU and providing a big texture you are able to minimize the free space in texture. For example loading an image of size 65 per 65 pixels will create a 128 per 128 pixels texture.