Cocos2d-x动画工具类

Cocos2d-x动画工具类,第1张

概述1.此工具类的目的是为了方便执行动画,使用TexturePackerGUI工具可以导出plist文件和png图片,这里我示例图片叫bxjg.plist和bxjg.png //////////////////////////////////////.h文件 #ifndef _AnimateUtil_H_ #define _AnimateUtil_H_ #include "cocos2d.h" usi

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,int Loops);
};

#endif

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

#include "AnimateUtil.h" Animation * AnimateUtil::createWithSingleFramename(const char * name,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 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",index++)); if (frame ==NulL) { break; } frameVec.pushBack(frame); } Animation * animation = Animation::createWithSpriteFrames(frameVec); animation->setLoops(Loops); animation->setRestoreOriginalFrame(true); animation->setDelayPerUnit(delay); return animation; }

总结

以上是内存溢出为你收集整理的Cocos2d-x动画工具类全部内容,希望文章能够帮你解决Cocos2d-x动画工具类所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1048664.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存