下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
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; }
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。