Cocos2d-x3.2 Animate帧动画

//GameScene.h

#include "cocos2d.h"

class GameScene : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();
    
    virtual bool init();
    
    void menuCallback(cocos2d::Ref* pSender);
    
    CREATE_FUNC(GameScene);
};




//GameScene.cpp

#include "GameScene.h"

USING_NS_CC;

cocos2d::Scene* GameScene::createScene()
{
    auto scene = Scene::create();   //创建一个场景
    auto layer = GameScene::create();   //创建一个图层
    scene->addChild(layer);
    return scene;
}

//初始化当前的图层
bool GameScene::init()
{
    if(!Layer::init())      //初始化父类
        return false;
    
    //获取屏幕大小
    Size visibleSize = Director::getInstance()->getVisibleSize();
    //auto size = Director::getInstance()->getWinSize();

    //帧动画的创建
    //方式一,通过多张图片来创建
    auto sprite1 = Sprite::create("grossini_dance_05.png");
    sprite1->setPosition(Vec2(visibleSize.width*0.3, visibleSize.height/2));
    this->addChild(sprite1);
    
    //创建帧动画序列,名词形式
    auto animation = Animation::create();
    for (int i=5; i<11; i++)
    {
        char szName[100] = {0};
        sprintf(szName, "grossini_dance_%02d.png", i);
        animation->addSpriteFrameWithFile(szName);
    }
    //设置帧动画属性
    animation->setDelayPerUnit(2.0f / 6);      //每一帧停留的时间,2秒时间完成6幅图片显示,切记要写成2.0f形式!
    animation->setRestoreOriginalFrame(true);       //播放完后回到第一帧
    
    auto animate = Animate::create(animation);
    sprite1->runAction(RepeatForever::create(animate));
    
    
    //方式二,通过一张集合的图片来创建
    //创建2D纹理
    auto texture = Director::getInstance()->getTextureCache()->addImage("dragon_animation.png");
    //建立图片帧
    auto frame0 = SpriteFrame::createWithTexture(texture, Rect(132*0, 132*0, 132, 132));
    auto frame1 = SpriteFrame::createWithTexture(texture, Rect(132*1, 132*0, 132, 132));
    auto frame2 = SpriteFrame::createWithTexture(texture, Rect(132*2, 132*0, 132, 132));
    auto frame3 = SpriteFrame::createWithTexture(texture, Rect(132*3, 132*0, 132, 132));
    auto frame4 = SpriteFrame::createWithTexture(texture, Rect(132*0, 132*1, 132, 132));
    auto frame5 = SpriteFrame::createWithTexture(texture, Rect(132*1, 132*1, 132, 132));
    
    auto sprite2 = Sprite::createWithSpriteFrame(frame0);
    sprite2->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
    this->addChild(sprite2);
    
    //保存图片帧
    //Vector<cocos2d::AnimationFrame *> array;
    Vector<cocos2d::SpriteFrame *> array;
    array.pushBack(frame0);
    array.pushBack(frame1);
    array.pushBack(frame2);
    array.pushBack(frame3);
    array.pushBack(frame4);
    array.pushBack(frame5);
    
    auto animation2 = Animation::createWithSpriteFrames(array, 0.2f);   //此处createWithSpriteFrames()函数确实每帧间隔时间参数,需自行加上去!!!
    sprite2->runAction(RepeatForever::create(Animate::create(animation2)));
    
    
    //方式三,通过.plist 文件来创建
    auto cache = SpriteFrameCache::getInstance();
    cache->addSpriteFramesWithFile("animate.plist");
    
    auto sprite3 = Sprite::createWithSpriteFrameName("grossini_dance_05.png");
    sprite3->setPosition(Vec2(visibleSize.width*0.7, visibleSize.height/2));
    this->addChild(sprite3);
    
    Vector<cocos2d::SpriteFrame *> arr;
    char str[100] = {0};
    for (int i=1; i<15; i++)
    {
        sprintf(str, "grossini_dance_%02d.png", i);
        auto frame_2 = cache->SpriteFrameCache::getInstance()->getSpriteFrameByName(str);   //3.2版本有改变
        arr.pushBack(frame_2);
    }
    
    auto animation3 = Animation::createWithSpriteFrames(arr, 0.2f);     //此处也不要忘记加上时间间隔参数
    sprite3->runAction(RepeatForever::create(Animate::create(animation3)));
    
    return true;
}

备注:注意spriteWithSpriteFrames()函数缺省时间间隔参数,需自行添加!同时,不要忘了把资源文件加载到Resources文件夹下

转载于:https://my.oschina.net/Jacedy/blog/301360

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值