Cocos2d-x動畫工具類

lidki 9年前發布 | 1K 次閱讀 C/C++ cocos2d-x

1.此工具類的目的是為了方便執行動畫,使用TexturePackerGUI工具可以導出plist文件和png圖片,這里我示例圖片叫bxjg.plist和bxjg.png

//////////////////////////////////////.h文件

ifndef _AnimateUtilH

define _AnimateUtilH

include "cocos2d.h"

using namespace cocos2d; using namespace std; class AnimateUtil//動畫工具類 { public: //根據文件名字前綴創建動畫對象 名稱 播放的間隔 是否循環播放
static Animation createWithSingleFrameName(const char name, float delay, int Loops); //根據文件名字前綴創建動畫對象,指定動畫圖片數量 名稱 圖片數量 播放的間隔 是否循環播放 static Animation createWithFrameNameAndNum(const char name, int num, float delay, int Loops); };

endif

/////////////////////////////////.cpp文件

include "AnimateUtil.h"

Animation AnimateUtil::createWithSingleFrameName(const char name, float delay, int Loops) { /將圖片加載到精靈幀緩沖池/ SpriteFrameCache frameCache = SpriteFrameCache::getInstance(); Vector<SpriteFrame > frameVec; 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<SpriteFrame > frameVec; 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; }</pre>

 本文由用戶 lidki 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!