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;
}