2012-02-22 57 views
0

我的問題是有關使用CCSpriteBatchNode的是如何工作的...當你與一個文件初始化CCSpriteBatchNode,讓我們說:的Cocos2D:CCSpriteBatchNode初始化

CCSpriteBatchNode *spriteBatch; 
spriteBatch = [CCSpriteBatchNode batchNodeWithFile:@"file.pvr.ccz"]; 
[self addChild:spriteBatch]; 

然後,這裏是我的疑問...爲什麼你需要添加你將使用的每個精靈到CCSpriteBatchNode,如果這些應該被加載,當你打電話batchNodeWithFile

在這裏你添加的每個精靈代碼:

NSArray *images = [NSArray arrayWithObjects:@"sprite1.jpg", @"sprite2.jpg", @"sprite3.jpg", @"sprite4.jpg", @"sprite5.jpg", @"sprite6.jpg", nil];  
for(int i = 0; i < images.count; ++i) { 
    NSString *image = [images objectAtIndex:i]; 
    float offsetFraction = ((float)(i+1))/(images.count+1);   
    CGPoint spriteOffset = ccp(winSize.width*offsetFraction, winSize.height/2); 
    CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:image]; 
    sprite.position = spriteOffset; 
    [spriteBatch addChild:sprite]; //Here is what I mean... Why to do this? Isn't that supposed that they are already loaded in the CCSpriteBatchNode? 
} 

謝謝!

回答

4

這是爲了優化。基本上當你添加一個CCSprite到一個圖層涉及到一個OpenGL調用來繪製這個項目(實際上他們是7我認爲),所以如果你有100個精靈,100個調用完成。如果將它們添加到BatchNode中,只需一次調用即可繪製其所有子項。

檢查文檔:

CCSpriteBatchNode就像一個批次節點:如果它包含的孩子,它會 吸引他們在1次單一的OpenGL調用(通常被稱爲「批抽獎」)。

CCSpriteBatchNode可以引用一個且只有一個紋理(一個圖像 文件,一個紋理圖集)。只有包含在 中的CCSprites可以將該紋理添加到CCSpriteBatchNode中。添加到CCSpriteBatchNode的所有CCSprites 都在一個OpenGL ES繪圖調用中繪製。如果 CCSprites未添加到CCSpriteBatchNode,則每個都需要一個OpenGL ES繪圖調用,效率較低。

+0

謝謝!現在我明白了!我很困惑:P – Axort 2012-02-22 18:51:53