使用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
类也是一个单例类,用于缓存所有的动画和动画帧。主要有以下成员函数:一般使用时,都是通过
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