1.此工具類的目的是為了方便執行動畫,使用TexturePackerGUI工具可以導出plist文件和png圖片,這里我示例圖片叫bxjg.plist和bxjg.png //////////////////////////////////////.h文件 #ifndef _AnimateUtil_H_ #define _AnimateUtil_H_ #include cocos2d.h us
1.此工具類的目的是為了方便執行動畫,使用TexturePackerGUI工具可以導出plist文件和png圖片,這里我示例圖片叫bxjg.plist和bxjg.png
//////////////////////////////////////.h文件
#ifndef _AnimateUtil_H_
#define _AnimateUtil_H_#endif
/////////////////////////////////.cpp文件
#include "AnimateUtil.h"
Animation * AnimateUtil::createWithSingleFrameName(const char * name, float delay, int Loops)
{
/*將圖片加載到精靈幀緩沖池*/
SpriteFrameCache *frameCache = SpriteFrameCache::getInstance();
Vector
SpriteFrame * frame = NULL;
int index = 1;//小圖片數量
do
{
//從SpriteFrame緩沖池獲取SpriteFrame對象
frame = frameCache->getSpriteFrameByName(StringUtils::format("%s%d.png", name, index++));
//不斷獲取spriteFrame對象,直到獲取的值為NULL
if (frame == NULL)
{
break;
}
frameVec.pushBack(frame);
} while (true);
//使用SpiteFrame列表創建動畫對象
Animation * animation = Animation::createWithSpriteFrames(frameVec);
animation->setLoops(Loops);//設置是否循環
animation->setRestoreOriginalFrame(true);
animation->setDelayPerUnit(delay);//設置動畫間隙
return animation;
}
Animation * AnimateUtil::createWithFrameNameAndNum(const char * name, int num, float delay, int Loops)
{
SpriteFrameCache * frameCache = SpriteFrameCache::getInstance();
SpriteFrame * frame = NULL;
Vector
int index = 1;
for (int i = 1; i <= num; i++)
{
frame = frameCache->getSpriteFrameByName(StringUtils::format("%s%d.png", name, index++));
if (frame ==NULL)
{
break;
}
frameVec.pushBack(frame);
}
Animation * animation = Animation::createWithSpriteFrames(frameVec);
animation->setLoops(Loops);
animation->setRestoreOriginalFrame(true);
animation->setDelayPerUnit(delay);
return animation;
}
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com