加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-x:自定义动画 使用plist来实现

发布时间:2020-12-14 17:00:10 所属栏目:百科 来源:网络整理
导读:使用plist文件,结合cocos2dx里面的动画类Animation来实现,这种方式比较推荐 代码如下: void BaseLayer::testTextture(){ Size visibleSize = Director::getInstance()-getVisibleSize(); VectorSpriteFrame* spriteFrameVec; auto spriteFrameCache = Spr


使用plist文件,结合cocos2dx里面的动画类Animation来实现,这种方式比较推荐



代码如下:


void BaseLayer::testTextture(){
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vector<SpriteFrame*> spriteFrameVec;
    auto spriteFrameCache = SpriteFrameCache::getInstance();
    
    // 这里使用plist文件
    spriteFrameCache->addSpriteFramesWithFile("test.plist","test.png");
    
    char path[256] = { 0 };
    for (int i = 1; i <= 6; ++i)
    {
        sprintf(path,"%d.png",i);
        log("path = %s",path);
        SpriteFrame *pSpriteFrame = spriteFrameCache->getSpriteFrameByName(path);
        spriteFrameVec.pushBack(pSpriteFrame);
    }
    
    // 0.1那个参数必须设定,可以设定除默认值意外的任何值,如果你不设定,根本就无法播放Animate动作
    // Cocos2d-x的坑还不少啊,各位需谨慎啊
    auto animation = Animation::createWithSpriteFrames(spriteFrameVec,0.1);
    animation->setDelayPerUnit(2.8f / 14.0f);//必须设置否则不会动态播放
    animation->setRestoreOriginalFrame(true);//是否回到第一帧
    animation->setLoops(-1);//重复次数 (-1:无限循环)
    
    auto sprite = Sprite::create();
        // 方法1:从SpriteFrameCache中根据名字获得对应的精灵帧,再设置
    sprite->setSpriteFrame(spriteFrameCache->getSpriteFrameByName("1.png"));
    
    // 方法2:从Animation中获得对应的帧,再进行设置
    AnimationCache::getInstance()->addAnimation(animation,"dogs");
    // sprite->setDisplayFrameWithAnimationName("dogs",0);
    
    sprite->setPosition(Vec2(visibleSize.width / 2,visibleSize.height / 2));
    addChild(sprite,1,1);
    
    FiniteTimeAction * animate = Animate::create(animation);
    sprite->runAction(animate);

}



官方文档:


You have to tell cocos2d-x which sprite frames should be used in an animation. The easiest way for this is to create a helper method that creates a list:

Vector HelloWorld::getAnimation(const char *format,int count)
{
    auto spritecache = SpriteFrameCache::getInstance();
    Vector animFrames;
    char str[100];
    for(int i = 1; i <= count; i++)
    {
        sprintf(str,format,i);
        animFrames.pushBack(spritecache->getSpriteFrameByName(str));
    }
    return animFrames;
}
This method retrieves the animation frames from the sprite sheet that follow a given format. You can call it in the following way:

Vector frames = getAnimation("capguy/walk/%04d.png",8);
The format string %04d creates 4 digit numbers prefixed with 0: 0001,0002,0003,...

The call returns a list of 8 sprite frames: capguy/walk/0001.png,... capguy/walk/0008.png

Add the following code block to the bottom of the init() method in HelloWorldScene.cpp before the return true:

// sprite
auto frames = getAnimation("capguy/walk/%04d.png",8);
auto sprite = Sprite::createWithSpriteFrame(frames.front());
background->addChild(sprite);
sprite->setPosition(100,620);

auto animation = Animation::createWithSpriteFrames(frames,1.0f/8);
sprite->runAction(RepeatForever::create(Animate::create(animation)));
The first line is the one you already know from above — to create the animation frames. The second creates a sprite using the first frame from the list.

You add the sprite as child of the background,not as child of the scene. This ensure that the sprite is always positioned in the right spot on the background.

与动画相关的类

  • AnimationCache
    AnimationCache类也是一个单例类,用于缓存所有的动画和动画帧。主要有以下成员函数:

    /** * 获得单个实例 */ static AnimationCache* getInstance(); /** * 释放这个 */void destroyInstance/** * 添加一个Animation对象,并指定一个名字 */ addAnimation(Animation animation,const std::string& name);/** * 从缓存中根据名字删除一个Animation */ removeAnimation(/** * 根据名字得到一个Animation对象 * 如果在缓存中没有对应名字的Animation对象,则返回NULL * 得到新的Animation对象之后,需要对应的调用retain函数 */Animation getAnimation/** * 从plist文件添加一个Animation对象 * 请确保在调用该函数之前,所有的帧已经被添加到SpriteFrameCache中了 */ addAnimationsWithFile plist);

    一般使用时,都是通过getInstance获得AnimationCache对象,然后通过addAnimation函数,加入Animation对象;在使用的时候,再通过getAnimation函数,传入Animation的名称,就可以从缓存中获得对应的Animation对象。

  • AnimationFrame
    和精灵帧SpriteFrame类似,动画帧也是单张的图片,也可以通过精灵帧进行定义。这货在实际开发中,我怎么用的不是很多啊,很多时候,都是SpriteFrame啊。
  • Animation
    Animation就是动画,存储一个动画动作需要的所有帧,一会在下面的实例中看代码吧。
  • Animate
    这个才是关键啊,你定义的动画帧或者精灵帧再多,没有动起来,也是徒劳啊。一切的关键就是Animate了。Animate动画动作就是一个动作类,继承自ActionInterval类。我们可以通过Animation来创建一个Animate,然后执行这个动作了。效果是什么样子的,就看下面的吧。

一个实例

总结了这么多了,该来一个实例了。看看效果吧。


参考地址:https://www.codeandweb.com/blog/2015/12/15/animations-and-spritesheets-in-cocos2d-x


对应资源下载地址: http://download.csdn.net/detail/zbjdsbj/9485017

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读