Cocos2d's CCSpriteBatchNode complaining about different texture ids
I'm trying to optimize my game using CCSpriteBatchNode to render multiple sprites at once. However, for some reason cocos2d is giving me this error when I introduce CCSpriteBatchNode as the parent for my sprites:
"CCSpr开发者_JS百科ite is not using the same texture id"
I'm confused by this because I have a single 1024x1024 texture atlas with all my graphics. It was created with TexturePacker and without the batch node, it all works. I've been loading it with this:
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"atlas1.plist"];
And now I'm trying to use CCSpriteBatchNode like this:
CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:@"something.png"];
CCSpriteBatchNode* bar = [CCSpriteBatchNode batchNodeWithFile:@"atlas1.png"];
[bar addChild: sprite]; // assert error here
I tried adding some NSLog debug output to cocos2d texture init/lookup code and surprisingly, there are some textures being created with different dimensions (for example 256x256 or even smaller). I don't understand how that can happen when I only have a single 1024x1024 png as the input.
What's happening? How can I debug this?
UPDATE:
Bongeh's answer helped me fix this - made me look twice at everything. There were some stale PNG files from an older version of the game in my iOS simulator that were being loaded, even though they were not in the Xcode project anymore. Doing a "Clean" or even "Clean build folder" from Xcode didn't work but using the "Reset content and settings" command from iOS simulator did the trick. Hooray!
If you have more than 1 spritesheet in your game, ensure that they do not share and framenames, another work around is to manually specify the texture id.
replace your above code with:
CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:@"something.png"];
CCSpriteBatchNode* bar = [CCSpriteBatchNode batchNodeWithFile:@"atlas1.png"];
[sprite setTexture:[bar texture]];
[bar addChild: sprite]; // assert error here
The only other thing I've discovered is sometimes the texturecache gets cleared if your game receives a memory warning in the application delegate, you can comment this out, but just be aware to clear the cache when you can.
精彩评论