Cocos帧动画Animate和Animation创建

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/amy260231120/article/details/52106665

本文为作者原创,其中知识内容出自闪电终结者的视频课程

首先用ppt简单的做一个帧动画,把做好的每一张图片命名为pass01.png pass02.png ….复制到res文件夹下

// 精灵是用于显示动画效果,需要写一个动画,然后往动画中添加图片,在让精灵显示出来
void TransferTestScene::spritePlayShow() {
    auto sprite = cocos2d::Sprite::create();
    // 设置锚点
    sprite -> setAnchorPoint(cocos2d::Vec2(0, 0));
    // 设置坐标
    sprite -> setPosition(0, 0);
    auto animation = cocos2d::Animation::create(); // 创建动画
    for (int index = 1; index <= 19; index++) {
        auto str = new char[50];// std::isstream << ""
        std::sprintf(str, "pass/pass%02d.png", index);
        animation -> addSpriteFrameWithFile(str); // 向动画添加图片
    }
    for (int index = 19; index >= 1; index--) {
        auto str = new char[50];// std::isstream << ""
        std::sprintf(str, "pass/pass%02d.png", index);
        animation -> addSpriteFrameWithFile(str); // 向动画添加图片
    }
    // 设置动画帧率
    animation -> setDelayPerUnit(5.0f / 38);
//    // 设置动画播放完毕后是否回到第一帧
//    animation -> setRestoreOriginalFrame(true);
    // 用动画创建精灵动作
    auto action = cocos2d::Animate::create(animation);
    // 让精灵来完成动作
    sprite -> runAction(cocos2d::RepeatForever::create(action));

    addChild(sprite);

Animation是由许多精灵帧组成,可以设置间隔时间,持续时间等,它实际上是包含着一组数据。Animate是一个动作,它是通过Animation对象创建。

猜你喜欢

转载自blog.csdn.net/amy260231120/article/details/52106665
今日推荐