Heim  >  Artikel  >  Datenbank  >  Cocos2d-x动画工具类

Cocos2d-x动画工具类

WBOY
WBOYOriginal
2016-06-07 15:24:351282Durchsuche

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_
#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 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 frameVec;
int index = 1;
for (int  i = 1; 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;
}

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn