Cocos2d-x动画工具类

1.此工具类的目的是为了方便运行动画。使用TexturePackerGUI工具能够导出plist文件和png图片,这里我演示样例图片叫bxjg.plist和bxjg.png

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

#ifndef _AnimateUtil_H_

#define _AnimateUtil_H_
#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;
}

原文地址:https://www.cnblogs.com/lytwajue/p/6724479.html