zoukankan      html  css  js  c++  java
  • Cocos2d-x动画工具类

    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<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 num, float delay, 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", 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;
    }

  • 相关阅读:
    二战后的一些战争启示(弱国无外交)
    为了生存人类必须去探索宇宙
    不同版本Eclipse对JDK版本要求
    string 转 java对象、转map的方式
    原生JS实现全选,反选
    oracle批量update
    HttpURLConnection 当作请求调用接口不带返回参数的工具类
    sun.misc.BASE64Encoder在Eclipse中不能直接使用的原因和解决方案
    javamail 发送邮件demo(文字与附件)
    Linux ping不通百度的解决方法
  • 原文地址:https://www.cnblogs.com/lytwajue/p/6724479.html
Copyright © 2011-2022 走看看