CCSprite: Batched sprites should use the same texture as the batchnode?

落爺英雄遲暮 提交于 2019-12-06 10:11:36

It simply means what it says : when you create the CCSpriteBatchNode object object with a texture, later when you add sprites to the CCSpriteBatchNode, all the sprites you add to the CCSpriteBatchNode MUST be from the same texture. For example:

NSString *spriteName = @"agility"; // an example, this code comes from a method that returns the batchNode
                                   // for many status types

NSString *plist = [NSString stringWithFormat:@"status_%@.plist",spriteName];
NSString *textureFileName = [NSString stringWithFormat:@"status_%@.pvr.gz",spriteName];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:plist textureFile:textureFileName];
CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:textureFileName];

//  plist has a frame named status_agility_Ok.png

CCSprite *frameStatusOk = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"status_%@_Ok.png",spriteName]];  
[batchNode addChild:frameStatusOk]; // this will work, both the sprite and the batch node have the same texture

CCSprite *frameStatusNotOk=[CCSprite spriteWithFile:@"someOtherTextureFile.pvr.gz"];
[batchNode addChild:frameStatusNotOk]; // this will fail an assertion and crash.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!