---
title: "CCSpriteBatchNode和CCTextureAtlas 详解-沈大海cocos2d-x教程21"
author: "mmdev"
date: "2012-11-30T15:43:00Z"
tags: []
slug: "ccspritebatchnode-cctextureatlas-cocos2d-x-21"
draft: false
lastmod: "2012-12-05T21:57:50Z"
id: 1742035
url: "https://mmdev.iteye.com/blog/1742035"
---

一般游戏图片资源会打包成一张大图，这样节省空间，又提升速度。打包工具有Zwoptex和texturepacker等等。

　　 CCSpriteBatchNode的初始化只要一张图片，也就是那张大图。然后把所有用到那张大图里面的小图的sprite都加到 CCSpriteBatchNode的child，绘制效率就会提高。下面是TestCpp中的Demo,SpriteTest

SpriteBatchNode1::SpriteBatchNode1()  
{  
setTouchEnabled( true );//让当前Layer接收触摸事件

//创建SpriteBatchNode对象和CCTextureAtlas的四边形的容量（缺省29）  
CCSpriteBatchNode* BatchNode = CCSpriteBatchNode::create("Images/grossini\_dance\_atlas.png", 50);

//首先要把batchNode添加到Layer 可以通过kTagSpriteBatchNode引用

addChild(BatchNode, 0, kTagSpriteBatchNode);  
CCSize s = CCDirector::sharedDirector()-&gt;getWinSize();  
addNewSpriteWithCoords( ccp(s.width/2, s.height/2) );//添加一个子Sprite，所有的Sprite是添加到batchNode的child Sprite  
}  
void SpriteBatchNode1::addNewSpriteWithCoords(CCPoint p)  
{  
CCSpriteBatchNode* BatchNode = (CCSpriteBatchNode\*) getChildByTag( kTagSpriteBatchNode );//得到BatchNode  
int idx = CCRANDOM\_0\_1() * 1400 / 100;  
int x = (idx%5) * 85;  
int y = (idx/5) * 121;  
CCSprite* sprite = CCSprite::createWithTexture(BatchNode-&gt;getTexture(), CCRectMake(x,y,85,121));//创建Sprite  
BatchNode-&gt;addChild(sprite); //**将创建好的Sprite添加到BatchNode**  
sprite-&gt;setPosition( ccp( p.x, p.y) );//设定坐标  
//处理动画

CCActionInterval* action;  
float random = CCRANDOM\_0\_1();  
if( random &lt; 0.20 )  
action = CCScaleBy::create(3, 2);  
else if(random &lt; 0.40)  
action = CCRotateBy::create(3, 360);  
else if( random &lt; 0.60)  
action = CCBlink::create(1, 3);  
else if( random &lt; 0.8 )  
action = CCTintBy::create(2, 0, -255, -255);  
else  
action = CCFadeOut::create(2);  
CCActionInterval* action\_back = action-&gt;reverse();  
CCActionInterval* seq = (CCActionInterval\*)(CCSequence::create(action, action\_back, NULL));  
sprite-&gt;runAction( CCRepeatForever::create(seq));//**在新创建的Sprite中应用动画**  
}  
void SpriteBatchNode1::ccTouchesEnded(CCSet* touches, CCEvent* event)  
{  
CCSetIterator it;  
CCTouch* touch;  
for( it = touches-&gt;begin(); it != touches-&gt;end(); it++)  
{  
touch = (CCTouch\*)(\*it);  
if(!touch)  
break;  
CCPoint location = touch-&gt;getLocation();  
addNewSpriteWithCoords( location );//在触摸点添加Sprite层  
}  
}

/////////////////////////////////////对SpriteBatchNode中的Sprite进行排序

void SpriteBatchNodeZOrder::reorderSprite(float dt)  
{  
CCSpriteBatchNode* batch= (CCSpriteBatchNode\*)(getChildByTag( kTagSpriteBatchNode ));  
CCSprite* sprite = (CCSprite\*)(batch-&gt;getChildByTag(kTagSprite1));  
int z = sprite-&gt;getZOrder();  
if( z &lt; -1 )  
m\_dir = 1;  
if( z &gt; 10 )  
m\_dir = -1;  
z += m\_dir * 3;  
batch-&gt;reorderChild(sprite, z);  
}

/////////////////////////////////对SpriteBatchNode中的Sprite进行remove和add

void SpriteBatchNodeColorOpacity::removeAndAddSprite(float dt)  
{ CCSpriteBatchNode* batch= (CCSpriteBatchNode\*)(getChildByTag( kTagSpriteBatchNode ));  
CCSprite* sprite = (CCSprite\*)(batch-&gt;getChildByTag(kTagSprite5));  
sprite-&gt;retain();  
batch-&gt;removeChild(sprite, false);  
batch-&gt;addChild(sprite, 0, kTagSprite5);  
sprite-&gt;release();  
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////

//在运行时动态修改纹理，适用于NPC升级，会影响所有的child Sprite

CCSpriteBatchNode* batch = (CCSpriteBatchNode\*) getChildByTag( kTagSpriteBatchNode );  
if( batch-&gt;getTexture() == m\_texture1 )  
batch-&gt;setTexture(m\_texture2);  
else  
batch-&gt;setTexture(m\_texture1);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//SpriteFrameCatcher与SpriteBatchNode结合使用

void SpriteFrameTest::onEnter()  
{  
SpriteTestDemo::onEnter();  
CCSize s = CCDirector::sharedDirector()-&gt;getWinSize();  
// IMPORTANT(重要声明):  
// The sprite frames will be cached AND RETAINED, and they won't be released unless you call

//SpriteFames被缓存和重新引用，需要在不用的适用调用下面方法释放这些SpriteFrames  
// CCSpriteFrameCache::sharedSpriteFrameCache()-&gt;removeUnusedSpriteFrames);  
CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();  
cache-&gt;addSpriteFramesWithFile("animations/grossini.plist");  
cache-&gt;addSpriteFramesWithFile("animations/grossini\_gray.plist", "animations/grossini\_gray.png");  
cache-&gt;addSpriteFramesWithFile("animations/grossini\_blue.plist", "animations/grossini\_blue.png");  
//  
// Animation using Sprite BatchNode 使用BatchNode创建动画  
m\_pSprite1 = CCSprite::createWithSpriteFrameName("grossini\_dance\_01.png");//从SpriteFameCache中创建一个Sprite  
m\_pSprite1-&gt;setPosition( ccp( s.width/2-80, s.height/2) );  
CCSpriteBatchNode* spritebatch = CCSpriteBatchNode::create("animations/grossini.png");//创建SpriteBatchNode 这里要和CCSpriteFrameCache一致  
spritebatch-&gt;addChild(m\_pSprite1);//将Sprite添加到SpriteBatchNode

addChild(spritebatch); //将SpriteBatchNode添加到当前layer  
CCArray* animFrames = CCArray::createWithCapacity(15);  
char str\[100] = {0};  
for(int i = 1; i &lt; 15; i++)  
{  
sprintf(str, "grossini\_dance\_%02d.png", i);  
CCSpriteFrame* frame = cache-&gt;spriteFrameByName( str ); //创建动画的多帧SpriteFrame  
animFrames-&gt;addObject(frame); //添加到CCArray集合  
}  
CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames, 0.3f);//创建动画对象

m\_pSprite1-&gt;runAction( CCRepeatForever::create( CCAnimate::create(animation) ) );//Sprite载入动画Action  
// to test issue #732, uncomment the following line  
m\_pSprite1-&gt;setFlipX(false);  
m\_pSprite1-&gt;setFlipY(false);  
//  
// Animation using standard Sprite，根据标准的Sprite创建动画  
//  
m\_pSprite2 = CCSprite::createWithSpriteFrameName("grossini\_dance\_01.png");  
m\_pSprite2-&gt;setPosition( ccp( s.width/2 + 80, s.height/2) );  
addChild(m\_pSprite2);  
CCArray* moreFrames = CCArray::createWithCapacity(20);  
for(int i = 1; i &lt; 15; i++)  
{  
sprintf(str, "grossini\_dance\_gray\_%02d.png",i);  
CCSpriteFrame \*frame = cache-&gt;spriteFrameByName(str);  
moreFrames-&gt;addObject(frame);  
}  
for( int i = 1; i &lt; 5; i++) {  
sprintf(str, "grossini\_blue\_%02d.png",i);  
CCSpriteFrame \*frame = cache-&gt;spriteFrameByName(str);  
moreFrames-&gt;addObject(frame);  
}  
// append frames from another batch  
moreFrames-&gt;addObjectsFromArray(animFrames);  
CCAnimation \*animMixed = CCAnimation::createWithSpriteFrames(moreFrames, 0.3f);  
m\_pSprite2-&gt;runAction(CCRepeatForever::create( CCAnimate::create(animMixed) ) );  
// to test issue #732, uncomment the following line  
m\_pSprite2-&gt;setFlipX(false);  
m\_pSprite2-&gt;setFlipY(false);

schedule(schedule\_selector(SpriteFrameTest::startIn05Secs), 0.5f);  
m\_nCounter = 0;  
}  
void SpriteFrameTest::onExit() //释放CCSpriteFrameCache中没有使用的SpriteFrame  
{  
SpriteTestDemo::onExit();  
CCSpriteFrameCache \*cache = CCSpriteFrameCache::sharedSpriteFrameCache();  
cache-&gt;removeSpriteFramesFromFile("animations/grossini.plist");  
cache-&gt;removeSpriteFramesFromFile("animations/grossini\_gray.plist");  
cache-&gt;removeSpriteFramesFromFile("animations/grossini\_blue.plist");  
}

CCTextureAtlas 纹理图集。说白了，它本身保存一张大图的纹理和一个记录画大图某一区域的信息结构体的数组。

由于CCTextureAtlas使用了opengl的VBO（顶点数组对象），所以用CCTextureAtlas所作的绘制在效率上比较高。

下面介绍一下该类的成员：

1.GLuint m\_pBuffersVBO\[2]，这是两个opengl VBO的句柄。一个用来做顶点数组句柄，一个用来做顶点索引数组句柄。

2.GLushort \*m\_pIndices，这是一个顶点索引数组的类本地备份。

3ccV3F\_C4B\_T2F\_Quad \*m\_pQuads ，这是一个顶点数组的类本地备份。这应该是CCTextureAtlas类实现的核心，所有该类的功能都再围绕该结构体在做操纵。

4.CCTexture2D \*m\_pTexture，这是保存大图的纹理对象。

5 int m\_uCapacity，四边形的容量。

6 int m\_uTotalQuads，实际上使用的四边形的数目。

7m\_bDirty ，当前opengl VBO中的数据是否是脏数据。为什么会存在这么一个类呢？实际上因为CCTextureAtlas容许你动态改变它管理的四边形数据，一旦你改变了该数据就需要一个标志位告诉该类在绘制四边形的时候把VBO中得数据更新掉。该成员就是起这个作用。