當我沒有優化的動畫,它的工作well.But當我優化我的動畫,程序crashed.And Xcode的日誌說:在優化動畫失敗
斷言失敗 - [CCSpriteBatchNode的addChild: Z:標籤:],/用戶/ hanpengbo /文檔/ Xcode中/ cocos2d_helloWorld/cocos2d_helloWorld /庫/椰油S2D/CCSpriteBatchNode.m:183
在CCSpriteBatchNode.m:183,有
NSAssert(child.texture.name == textureAtlas_.texture.name, @"CCSprite is not using the same texture id");
這裏是我的代碼
// cache
CCSpriteFrameCache *cache=[CCSpriteFrameCache sharedSpriteFrameCache];
[cache addSpriteFramesWithFile:@"birdAtlas.plist"];
// frame array
NSMutableArray *framesArray=[NSMutableArray array];
for (int i=1; i<10; i++) {
NSString *frameName=[NSString stringWithFormat:@"bird%d.png", i];
id frameObject=[cache spriteFrameByName:frameName];
[framesArray addObject:frameObject];
}
// animation object
id animObject=[CCAnimation animationWithFrames:framesArray delay:0.1];
// animation action
id animAction=[CCAnimate actionWithAnimation:animObject restoreOriginalFrame:NO];
animAction=[CCRepeatForever actionWithAction:animAction];
// sprite
cocosGuy = [CCSprite spriteWithFile: @"Icon.png"];//cocosGuy is CCSprite,declared earler
cocosGuy.position = ccp(200, 300);
// batchNode
CCSpriteBatchNode *batchNode=[CCSpriteBatchNode batchNodeWithFile:@"birdAtlas.png"];
[self addChild:batchNode];
[batchNode addChild:cocosGuy];
[cocosGuy runAction:animAction];
UPDATE: 這裏是更正後的代碼,和它運作良好
// batchNode
CCSpriteBatchNode *batchNode=[CCSpriteBatchNode batchNodeWithFile:@"birdAtlas.png"];
[cocosGuy setTexture:[batchNode texture]];
[self addChild:batchNode];
[batchNode addChild:cocosGuy];
BatchNode有幾個要求。崩潰的常見原因包括:inited,在線程中調用batchnode或batchnode紋理已被回收。在你的情況下,你可能想在某處保留'batchNode'的引用,作爲一個實例變量。不要讓它破壞。 – 2013-02-13 13:49:09
在Xcode中啓用全局異常斷點,這將向您顯示幫助確定問題的崩潰行(在此處複製粘貼失敗的斷言行)。最重要的是檢查所有動畫幀是否在birdAtlas.png中,而不是在不同的紋理地圖集中。 – LearnCocos2D 2013-02-13 14:01:06
***聲明失敗 - [CCSpriteBatchNode addChild:z:tag:],/Users/hanpengbo/Documents/Xcode/cocos2d_helloWorld/cocos2d_helloWorld/libs/cocos2d/CCSpriteBatchNode.m:183 – 2013-02-13 14:30:45