2013-02-13 91 views
1

當我沒有優化的動畫,它的工作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]; 
+0

BatchNode有幾個要求。崩潰的常見原因包括:inited,在線程中調用batchnode或batchnode紋理已被回收。在你的情況下,你可能想在某處保留'batchNode'的引用,作爲一個實例變量。不要讓它破壞。 – 2013-02-13 13:49:09

+0

在Xcode中啓用全局異常斷點,這將向您顯示幫助確定問題的崩潰行(在此處複製粘貼失敗的斷言行)。最重要的是檢查所有動畫幀是否在birdAtlas.png中,而不是在不同的紋理地圖集中。 – LearnCocos2D 2013-02-13 14:01:06

+0

***聲明失敗 - [CCSpriteBatchNode addChild:z:tag:],/Users/hanpengbo/Documents/Xcode/cocos2d_helloWorld/cocos2d_helloWorld/libs/cocos2d/CCSpriteBatchNode.m:183 – 2013-02-13 14:30:45

回答

1

對於這個工作,你的icon.png質地應在birdAtlas.png紋理,並在.plist中進行適當的聲明。 Batchnodes 1)是用一個紋理(只有一個)創建的,並且2)只接受來自同一個紋理的小精靈。

...而且,我不知道你的意圖,但通常你會

CCSprite *cocosGuy = [CCSprite spriteWithSpriteFrame:[cache spriteFrameByName:@"bird1.png"]; 
在這種情況下

,我想批量添加節點會工作。

...並且,如果紋理僅包含該動畫的動畫幀,則不確定使用批處理節點對於動畫將具有任何後果。一次只顯示一個框架,所以我不認爲你會從批量繪製調用中受益。

+0

「Icon.png紋理應該在birdAtlas.png紋理中」。謝謝,我不明白這是什麼意思。紋理是什麼? – 2013-02-13 14:41:47

+0

紋理基本上是一個圖形內容(如PNG)。地圖集是包含多個圖像的紋理。您的birdAtlas可能擁有動畫所需的全部10張圖像。圖集也可以用於將多個圖像分組在一個紋理中。請記住,在使用批處理節點時,添加到批處理節點的所有子節點必須來自相同的紋理(通常爲.png文件或其他圖形格式),批處理節點必須使用相同的紋理創建。 – YvesLeBorg 2013-02-13 15:01:42

+0

謝謝,我解決了這個問題 – 2013-02-13 15:26:58