问题
I keep getting a crash that says: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'CCSprite: Batched sprites should use the same texture as the batchnode'
I am not quite sure what this means. I googled the crash but got no results. Also this only happens when I go back to my first scene after coming back from the second scene in my game.
I double checked my code and I make sure all the images in my sprite sheets are added as children to the batch node. I also make sure the images in my app that aren't in my sprite sheet are added as a child to my layer and not the batch node.
Anyway what causes this crash and how can I fix it?
Thanks!
Edit1: It seems to be happening on this line in my app:
[self.spriteLeft setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:imageName]];
I NSLog imageName and it returns: MyImage.png , then I look in my plist and pvr.ccz that I load and MyImage.png is definitely in there. So I don't know why it is crashing when everything looks right. Also I make sure that I do not use spriteWithFile anywhere where I load an image thats in my sprite sheet.
回答1:
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.
来源:https://stackoverflow.com/questions/12336478/ccsprite-batched-sprites-should-use-the-same-texture-as-the-batchnode