创建帧动画

一、步骤

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);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值