我们用Silverlight框架开发WP8的应用程序的时,编写动画可以使用缓动效果来实现缓动动画对吧,那么在Cocos2d-x框架里面我们一样是可以缓动动作(缓动动画),其实技术的东西都是想通的,如果你很好地理解了缓动动画,那么对于Cocos2d-x框架的缓动动作其实就是一碟小菜了。那么对于缓动动作我们可以理解为给普通的动作加上了一个加速度,让其运动的效果更加符合我们的自然规律。那么在Cocos2d-x框架里面内置了一系列常用的缓动动作,我们来看下下面的这图类的继承图。
头文件:
#include <CCActionEase.h>
继承关系图:
示例代码:
//在init方法里面初始化当前的实例 bool HelloWorld::init() { bool bRet = false; do { //CCLayer进行初始化,初始化失败跳出循环 if ( !CCLayer::init() ) { break; } //获取手机屏幕的大小 CCSize size = CCDirector::sharedDirector()->getWinSize(); //创建精灵1 CCSprite *cat1 = CCSprite::create("cat.png"); cat1->setPosition(ccp(0, 0)); cat1->setPosition(ccp(100, 200)); this->addChild(cat1); //创建精灵2 CCSprite *cat2 = CCSprite::create("cat.png"); cat2->setPosition(ccp(0, 0)); cat2->setPosition(ccp(100, 300)); this->addChild(cat2); //创建精灵3 CCSprite *cat3 = CCSprite::create("cat.png"); cat3->setPosition(ccp(0, 0)); cat3->setPosition(ccp(100,400)); this->addChild(cat3); //匀速的移动动作 CCActionInterval* move = CCMoveBy::create(3, CCPointMake(800,0)); CCActionInterval* move_back = move->reverse(); CCFiniteTimeAction* seq1 = CCSequence::create( move, move_back, NULL); cat1->runAction( CCRepeatForever::create((CCActionInterval*)seq1)); //添加EaseBackIn缓动效果的移动动画 CCActionInterval* move_ease_in = CCEaseBackIn::create((CCActionInterval*)(move->copy()->autorelease()) ); CCActionInterval* move_ease_in_back = move_ease_in->reverse(); CCFiniteTimeAction* seq2 = CCSequence::create( move_ease_in, move_ease_in_back, NULL); cat2->runAction( CCRepeatForever::create((CCActionInterval*)seq2)); //添加EaseBackOut缓动效果的移动动画 CCActionInterval* move_ease_out = CCEaseBackOut::create((CCActionInterval*)(move->copy()->autorelease()) ); CCActionInterval* move_ease_out_back = move_ease_out->reverse(); CCFiniteTimeAction* seq3 = CCSequence::create( move_ease_out, move_ease_out_back, NULL); cat3->runAction( CCRepeatForever::create((CCActionInterval*)seq3)); //接收界面的的触摸事件 setTouchEnabled(true); bRet = true; } while (0); //返回成功 return bRet; }
运行的效果,注意观察使用了缓动动作的精灵的运动情况: