一般游戏图片资源会打包成一张大图,这样节省空间,又提升速度。打包工具有Zwoptex和texturepacker等等。
CCSpriteBatchNode的初始化只要一张图片,也就是那张大图。然后把所有用到那张大图里面的小图的sprite都加到 CCSpriteBatchNode的child,绘制效率就会提高。1) 缓冲sprite帧和纹理
// 从纹理贴图集中预加载精灵帧,这个方法做了以下几件事:寻找工程目录下面和输入的参数名字一样,但是后缀是.png的图片文件。然后把这个文件加入到共享的CCTextureCache中。
解析plist文件,追踪所有的sprite在spritesheet中的位置,内部使用CCSpriteFrame对象来追踪这些信息。
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"table.plist"];
2) 创建一个精灵批处理结点
// 为所有 静态刚体 设置的批处理精灵节点
CCSpriteBatchNode* batch = [CCSpriteBatchNode batchNodeWithFile:@"table.png"];
[self addChild:batch];
CCSprite* tableTop = [CCSprite spriteWithSpriteFrameName:@"tabletop.png"]; tableTop.position = [Helper screenCenter]; [batch addChild:tableTop];
-(CCSpriteBatchNode*) getSpriteBatch { return (CCSpriteBatchNode*)[self getChildByTag:kTagBatchNode]; } CCSpriteBatchNode* batch = [[PinballTable sharedTable] getSpriteBatch]; sprite = [CCSprite spriteWithSpriteFrameName:spriteFrameName]; [batch addChild:sprite];
//Batch是一个CCSpriteBatchNode,下面Batch-》Sprite-》ChildrenSprite,
//它们的Tag分别为:TagofBatch,TagofSprite,TagofChildrenSprite 得到ChildSprite的方法如下,
根据各自的Tag得到相应的数据。
id Batch = [self getChildByTag:TagofBatch]; id Sprite = [Batch getChildByTag:TagofSprite]; CCSprite *ChildrenSprite = (CCSprite*) [Sprite getChildByTag:TagofChildrenSprite];