TexturePacker 它是一款把若干资源图片拼接为一张大图的工具。
我们知道openGL载入纹理图片的时候,所用内存是会自动扩张到2的n次方的。
所以我们需要把很多小图拼接到一张大图里,
然后到代码中取其所需。
这样,浪费掉的空间就很小了。
还有就是,提前一次载入,减少I/O,提高速度。
综上:
节省内存,提高效率。
生成的XX.plist文件,XX.png图片一张。
如何获取XX.png这张图片上的小图片呢?
很简单,知道小图片的Name就行。
我们可以根据plist文件获取单个CCSprite,把他添加到CCLayer上。
方法如下:
CCSprite* HelloWorld::addSp(const char*ImageName)
{
CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile("TankUIp.plist", "TankUIp.png");
CCString::createWithFormat(ImageName)->getCString();
CCSpriteFrame*frame = cache->spriteFrameByName(ImageName);
CCSprite*sp = CCSprite::createWithSpriteFrame(frame);
return sp;
}
bool HelloWorld::init()
{
CCSprite*sp = addSp("1P.png");
sp->setPosition(ccp(200,400));
this->addChild(sp);
return true;
}
还可以通过plist文件保存的序列帧创建Animation
方法如下:
static void buildAnimateByPlist(CHAR* plistName, int beginFrame,int endFrame,CHAR* findpngName,CHAR* setAniName,bool B,double frameSpeed,cocos2d::CCObject * target,cocos2d::SEL_CallFuncO selector)
{
int nNameLength = strlen(plistName);
char * pImageName = new char[nNameLength+5];
strcpy(pImageName,plistName);
strcat(pImageName,".png");
char * pPlistName = new char[nNameLength+6];
strcpy(pPlistName,plistName);
strcat(pPlistName,".plist");
CCSpriteFrameCache *frameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
frameCache->addSpriteFramesWithFile(pPlistName);
CCArray* animFrames = CCArray::createWithCapacity(endFrame+1);
char str[100] = {0};
for(int i = beginFrame; i < endFrame+1; i++)
{
sprintf(str, findpngName,i);
CCSpriteFrame *frame = frameCache->spriteFrameByName(str);
animFrames->addObject(frame);
}
//设置动画名字
CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames, frameSpeed);
//加入缓存
CCAnimationCache::sharedAnimationCache()->addAnimation(animation, setAniName);
//CCAnimationCache *animCache = CCAnimationCache::sharedAnimationCache();
//CCAnimation *action = animCache->animationByName(setAniName);
//return action;
CCTextureCache::sharedTextureCache()->addImageAsync(pImageName,target,selector);
}
static CCAnimation * getAnimationByName(CHAR* setAniName,bool isRestoreOrgin = true)
{
CCAnimationCache *animCache = CCAnimationCache::sharedAnimationCache();
CCAnimation * animation = animCache->animationByName(setAniName);
animation->setRestoreOriginalFrame(isRestoreOrgin);
return animation;
}
void HelloWorld::loadingProgress(cocos2d::CCObject * obj)
{
//在此处添加进度条Animation
cocos2d::CCSprite * sp = cocos2d::CCSprite::create();
sp->initWithFile("CloseNormal.png");
sp->setPosition(ccp(100,200));
this->addChild(sp);
}
bool HelloWorld::init()
{
MyTools::buildAnimateByPlist("Animation",0,11,"EnemyBullet%02d.png","ebullet",false,0.12,this,callfuncO_selector(HelloWorld::loadingProgress));
CCAnimation * pre_ani_atk =MyTools::getAnimationByName("ebullet");
CCAnimate*actoinwq = CCAnimate::create(pre_ani_atk);
CCSprite*m_Bullet = CCSprite::create("hedan.png",CCRect(0,0,30,70));
m_Bullet->setPosition(ccp(200,200));
this->addChild(m_Bullet);
m_Bullet->runAction(actoinwq);
return true;
}