精灵的概念这里就不说了,论坛上有很多。
一、今天的问题是:如果我有一套美术资源,如一个羊的素材(当然是png),它有站立 ,有跑步,有行走动作。如果当前要加载两个这样的羊,我们该怎么做?
1.第一种方法,想必大家都知道 加载2次。
当然这样有的话,如果我要100个难道也要加载100次的Animation吗?显然不是
2.第二种方法 这里我就按照自己的理解注释了
#define SHEEP_STAND "sheep_stand"
#define SHEEP_RUN "sheep_run"
#define SHEEP_BATTACK "sheep_battack"
//创建帧缓冲
CCSpriteFrameCache *sheepFrameCache =CCSpriteFrameCache::sharedSpriteFrameCache();
sheepFrameCache->addSpriteFramesWithFile("sheep.plist");
//创建动作缓冲
CCAnimationCache *sheepAniCache = CCAnimationCache::sharedAnimationCache();
cocos2dx中CCAnimationCache是单例 可以直接取到不用创建
CCAnimationCache *SpriteAni = CCAnimation::create();
for(int i=1;i<7;i++)
{
char szName[100] = {0};
sprintf(szName, "stand/stand%d.png",i);
CCLog("%s",szName);
CCSpriteFrame *SFrame = sheepFrameCache->spriteFrameByName(szName);
SpriteAni->addSpriteFrame(SFrame);
}
sheepAniCache->addAnimation(SpriteAni, SHEEP_STAND);
这样我就加入到了动作缓冲中 下次要是再使用的时候就可以:
SpriteAni = sheepAniCache->animationByName(SHEEP_STAND);
OK 怎么加载动作的话大家一概自己清楚
CCFiniteTimeAction *action = CCSequence::create(aniStart, CCAnimate::create(SpriteAni,aniEnd,NULL);
然后runAction就可以了!
二、减少绘制 引入CCSpriteBatchNode类
CCSpriteBatchNode *parent2 = CCSpriteBatchNode::create("sheep.pvr.ccz");
addChild(parent2);
CCSprite *sh1 = CCSprite::create("1.png");
parent2->addChild(sh1);
CCSprite *sh2 = CCSprite::create("2.png");
parent2->addChild(sh2);
cocos2dx作者解释:
// Animation using Sprite batch
//
// A CCSpriteBatchNode can reference one and only one texture (one .png file)
// Sprites that are contained in that texture can be instantiatied as CCSprites and then added to the CCSpriteBatchNode
// All CCSprites added to a CCSpriteBatchNode are drawn in one OpenGL ES draw call
// If the CCSprites are not added to a CCSpriteBatchNode then an OpenGL ES draw call will be needed for each one, which is less efficient