例子1:
CCSpriteBatchNode* batch = [CCSpriteBatchNode batchNodeWithFile:@"table.png"];
[self addChild:batch];
- 創建一個CCSpriteBatchNode對象,通過傳遞一個包含所有sprite的batch的名字作為參數,並把它加入到當前場景之中。
- 接下來,你從batch中創建的任何sprite,你應該把它當作CCSpriteBatchNode的一個孩子加進去。只要sprite包含在batch中,那么就沒問題,否則會出錯。后面加的CCSprite的名字,要不跟 batchNode創建的時候是同一個圖片名字,要不就是 CCSpriteFrameCache里面的大圖的小圖的名字,否則會有問題。其實就是同一個紋理貼圖 CCTexture2D ,比如下面的tabletop.png必須是 table.png的小圖。
- CCSprite* tableTop = [CCSprite spriteWithSpriteFrameName:@"tabletop.png"];
tableTop.position = [Helper screenCenter];
[batch addChild:tableTop]; - CCSpriteBatchNode可以智能地遍歷它的所有的孩子結點,並通過一次OpenGL ES call來渲染這些孩子,而不是以前每個sprite都需要一個OpenGL call,這樣渲染速度就會更快。
例子2:
CCSpriteBatchNode* batchNode = [CCSpriteBatchNode batchNodeWithFile:@"bullet.png"];
[self addChild:batchNode];
for (int i = 0; i < 500; ++i)
{
- CCSprite* bullet = [CCSprite spriteWithFile:@"bullet.png"];
- [batchNode addChild:bullet];
- }
1. 創建
CCSpriteBatchNode* create(const char* fileImage, unsigned intcapacity);
CCSpriteBatchNode* create(const char* fileImage);
第一個是 傳入 文件名,和容量。
第二個是直接傳入文件名,容量是默認的kDefaultSpriteBatchCapacity 值為29
也可以通過 CCSpriteBatchNode* createWithTexture(CCTexture2D* tex,unsigned int capacity);
CCSpriteBatchNode* createWithTexture(CCTexture2D* tex);這兩個方法傳入 CCTexture2D對象來創建。
2. 加入子節點
virtual void addChild(CCNode * child);
virtual void addChild(CCNode * child, int zOrder);
virtual void addChild(CCNode * child, int zOrder, int tag);
3. 重新改變zorder
virtual void reorderChild(CCNode * child, int zOrder);
4. 移除
virtual void removeChild(CCNode* child, bool cleanup);
virtual void removeAllChildrenWithCleanup(bool cleanup);