Cocos2d-x动画工具类
发布时间:2020-12-16 07:48:47 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 1.此工具类的目的是为了方便执行动画,使用TexturePackerGUI工具可以导出plist文件和png图片,这里我示例图片叫bxjg.plist和bxjg.png////////////////
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 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; } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |