创建帧动画

来源:互联网 发布:达梦数据库客户端工具 编辑:程序博客网 时间:2024/06/08 10:26

一、步骤

1.1 将多张散图打印成一张大图

1.2 使用精灵帧缓存将大图中的小图读取到缓存中

1.3 将多张精灵帧创建动画帧

1.4 将多张动画帧创建成动画

1.5 将动画转成动作

1.6 让精灵执行这个动作


二、代码实例

    auto sp=Sprite::create("21.png");

    sp->setPosition(Vec2(480,320));

    addChild(sp);

    // 获取精灵帧缓存

    auto cache=SpriteFrameCache::getInstance();

    // 通过plist文件及大图,将每一张小图的精灵帧添加到缓存中

    cache->addSpriteFramesWithFile("hero.plist","hero.png");

    // 创建一个vector,用来存放精灵帧

    Vector<SpriteFrame *>vec;

    // 创建一个字符数组,用来保存图片名

    char str[10];

    

    for (int i=1; i<=8; i++) {

        // 将图片名称保存到str

        sprintf(str,"2%d.png",i);

        // 通过str(图片名),从精灵帧缓存中获取精灵帧

        auto spriteFrame=cache->getSpriteFrameByName(str);

        // 获取到的精灵帧保存到 vec

        vec.pushBack(spriteFrame);

    }

    // 使用获取到的精灵帧来创建动画,param:Vector &、帧间隔、循环次数

    auto animation=Animation::createWithSpriteFrames(vec,0.2f,1);

    // 使用帧动画来创建动作

    auto animate=Animate::create(animation);

    auto repeat=RepeatForever::create(animate);

    sp->runAction(repeat);


0 0
原创粉丝点击