今天,我带领大家学习一下Cocos2d-x 2.0的序列帧动画。在Cocos2d-x中,提供了相应的一些类和方法,可以方便的生成序列帧动画,这样我们就可以制做各种人物动作以及动画效果。这就是鱼在水里游动的基础动画。
序列帧动画主要有几个类:
CCSpriteFrame:精灵帧信息,序列帧动画是依靠多个精灵帧信息来显示相应的纹理图像,一个精灵帧信息包包含了所使用的纹理,对应纹理块的位置以及纹理块是否经过旋转和偏移,这些信息可以取得对应纹理中正确的纹理块区域做为精灵帧显示的图像。
CCAnimationFrame:序列帧动画单帧信息,它存储了对应的精灵帧信息。
CCAnimation:序列帧动画信息,它存储了所有的单帧信息,可以对单帧信息进行管理。
CCAnimate:序列帧动画处理类,它是真正完成动画表演的类。
void GameScene::fishAnim(){ <span style="white-space:pre"> </span>SpriteFrameCache *frameCache = SpriteFrameCache::getInstance(); frameCache->addSpriteFramesWithFile("fish.plist", "fish.png"); //加载Plist缓存 Size visibleSize = Director::getInstance()->getVisibleSize(); auto fish = Sprite::createWithSpriteFrameName("fish1.png"); //创建一个鱼的首个帧 fishVec.pushBack(fish); //放进数组 fish->setPosition(Vec2(dd, dt)); //设置坐标 this->addChild(fish, 5); //添加到场景 Animation* animation = Animation::create(); //创建序列帧动画 animation->setDelayPerUnit(0.01f); //0.01s播放完序列帧动画 animation->addSpriteFrame(frameCache1->getSpriteFrameByName("fish1.png")); animation->addSpriteFrame(frameCache1->getSpriteFrameByName("fish2.png")); animation->addSpriteFrame(frameCache1->getSpriteFrameByName("fish3.png")); //3个序列帧动画顺序执行 Animate* animate = Animate::create(animation); //帧动画处理 fish->runAction(Sequence::create(animate, CallFunc::create(CC_CALLBACK_0(GameScene::remove, this)), nullptr)); }最后那个remove回调函数是用于删除动画,优化游戏环境。