cocos2d-x for wp之EaseActions——运动加速效果
在现实生活中有很多情况下,物体的运动都是带有加速度的运动。如果只是凭借cocos2d-x(wp)里的CCActionInstanse(瞬时动作的基类)和CCActionInterval(延时动作的基类)是无法达到“逼真”的现实运动效果的。
所以在cocos2d-x 里,有一个Ease系列
在Ease的方法里改变了运动的速度,但是并没有改变总体时间。如果整个的action持续5秒钟,那么整个的时间仍然会持续5秒钟。
这些action可以被分成3类:
In actions: action开始的时候加速
Out actions: action结束的时候加速
InOut actions: action开始,结束的时候加速
在Ease系列里的第一个参数为要加减速度的动作,第二个为加减的速率。
下面是个简单的例子:(m_grossini、m_tamara、m_kathia均为精灵)
CCActionInterval actionBy = CCJumpBy.actionWithDuration(2, new CCPoint(300, 100), 50, 4); m_grossini.runAction(CCEaseIn.actionWithAction(actionBy, 2f));
还有一些特殊的缓冲:
1.指数缓冲(效果:如同指数函数一样,在开头或者结尾或者中间突然获得指数型的加速度)
CCEaseExponentialIn
CCEaseExponentialOut
CCEaseExponentialInOut
CCActionInterval move = CCMoveBy.actionWithDuration(3, new CCPoint(350, 0)); CCActionInterval move_back = move.reverse() as CCActionInterval; CCActionInterval move_ease_in = CCEaseExponentialIn.actionWithAction((CCActionInterval)(move.copy())); CCActionInterval move_ease_in_back = move_ease_in.reverse() as CCActionInterval; CCActionInterval move_ease_out = CCEaseExponentialOut.actionWithAction((CCActionInterval)(move.copy())); CCActionInterval move_ease_out_back = move_ease_out.reverse() as CCActionInterval; CCFiniteTimeAction seq1 = CCSequence.actions(move, move_back); CCFiniteTimeAction seq2 = CCSequence.actions(move_ease_in, move_ease_in_back); CCFiniteTimeAction seq3 = CCSequence.actions(move_ease_out, move_ease_out_back); m_grossini.runAction(CCRepeatForever.actionWithAction((CCActionInterval)seq1)); m_tamara.runAction(CCRepeatForever.actionWithAction((CCActionInterval)seq2)); m_kathia.runAction(CCRepeatForever.actionWithAction((CCActionInterval)seq3));
2.赛因缓冲(效果:可以理解为头尾较为平缓、中间为加速度恒定的匀变速运动)
CCEaseSineIn
CCEaseSineOut
CCEaseSineInOut
CCActionInterval move = CCMoveBy.actionWithDuration(3, new CCPoint(350, 0)); CCActionInterval move_back = move.reverse() as CCActionInterval; CCActionInterval move_ease_in = CCEaseSineIn.actionWithAction((CCActionInterval)(move.copy())); CCActionInterval move_ease_in_back = move_ease_in.reverse() as CCActionInterval; CCActionInterval move_ease_out = CCEaseSineOut.actionWithAction((CCActionInterval)(move.copy())); CCActionInterval move_ease_out_back = move_ease_out.reverse() as CCActionInterval; CCFiniteTimeAction seq1 = CCSequence.actions(move, move_back); CCFiniteTimeAction seq2 = CCSequence.actions(move_ease_in, move_ease_in_back); CCFiniteTimeAction seq3 = CCSequence.actions(move_ease_out, move_ease_out_back); m_grossini.runAction(CCRepeatForever.actionWithAction((CCActionInterval)seq1)); m_tamara.runAction(CCRepeatForever.actionWithAction((CCActionInterval)seq2)); m_kathia.runAction(CCRepeatForever.actionWithAction((CCActionInterval)seq3));
3.弹性缓冲(效果:类似于弹了一下一根绷紧的橡皮筋。其实这个效果看名字就知道了)
CCEaseElasticIn
CCEaseElasticOut
CCEaseElasticInOut
CCActionInterval move = CCMoveBy.actionWithDuration(3, new CCPoint(350, 0)); CCActionInterval move_back = move.reverse() as CCActionInterval; CCActionInterval move_ease_in = CCEaseElasticIn.actionWithAction((CCActionInterval)(move.copy())); CCActionInterval move_ease_in_back = move_ease_in.reverse() as CCActionInterval; CCActionInterval move_ease_out = CCEaseElasticOut.actionWithAction((CCActionInterval)(move.copy())); CCActionInterval move_ease_out_back = move_ease_out.reverse() as CCActionInterval; CCFiniteTimeAction seq1 = CCSequence.actions(move, move_back); CCFiniteTimeAction seq2 = CCSequence.actions(move_ease_in, move_ease_in_back); CCFiniteTimeAction seq3 = CCSequence.actions(move_ease_out, move_ease_out_back); m_grossini.runAction(CCRepeatForever.actionWithAction((CCActionInterval)seq1)); m_tamara.runAction(CCRepeatForever.actionWithAction((CCActionInterval)seq2)); m_kathia.runAction(CCRepeatForever.actionWithAction((CCActionInterval)seq3));
4.跳跃缓冲(效果:可以理解为一个球从空中落地之后在地上弹跳的情况)
CCEaseBounceIn
CCEaseBounceOut
CCEaseBounceInOut
CCActionInterval move = CCMoveBy.actionWithDuration(3, new CCPoint(350, 0)); CCActionInterval move_back = move.reverse() as CCActionInterval; CCActionInterval move_ease_in = CCEaseBounceIn.actionWithAction((CCActionInterval)(move.copy())); CCActionInterval move_ease_in_back = move_ease_in.reverse() as CCActionInterval; CCActionInterval move_ease_out = CCEaseBounceOut.actionWithAction((CCActionInterval)(move.copy())); CCActionInterval move_ease_out_back = move_ease_out.reverse() as CCActionInterval; CCFiniteTimeAction seq1 = CCSequence.actions(move, move_back); CCFiniteTimeAction seq2 = CCSequence.actions(move_ease_in, move_ease_in_back); CCFiniteTimeAction seq3 = CCSequence.actions(move_ease_out, move_ease_out_back); m_grossini.runAction(CCRepeatForever.actionWithAction((CCActionInterval)seq1)); m_tamara.runAction(CCRepeatForever.actionWithAction((CCActionInterval)seq2)); m_kathia.runAction(CCRepeatForever.actionWithAction((CCActionInterval)seq3));
5.回震缓冲(效果:跟跳跃缓冲差不多,不过回震可以看成类似完全弹性碰撞的结果)
CCEaseBackIn
CCEaseBackOut
CCEaseBackInOut
CCActionInterval move = CCMoveBy.actionWithDuration(3, new CCPoint(350, 0)); CCActionInterval move_back = move.reverse() as CCActionInterval; CCActionInterval move_ease_in = CCEaseBackIn.actionWithAction((CCActionInterval)(move.copy())); CCActionInterval move_ease_in_back = move_ease_in.reverse() as CCActionInterval; CCActionInterval move_ease_out = CCEaseBackOut.actionWithAction((CCActionInterval)(move.copy())); CCActionInterval move_ease_out_back = move_ease_out.reverse() as CCActionInterval; CCFiniteTimeAction seq1 = CCSequence.actions(move, move_back); CCFiniteTimeAction seq2 = CCSequence.actions(move_ease_in, move_ease_in_back); CCFiniteTimeAction seq3 = CCSequence.actions(move_ease_out, move_ease_out_back); m_grossini.runAction(CCRepeatForever.actionWithAction((CCActionInterval)seq1)); m_tamara.runAction(CCRepeatForever.actionWithAction((CCActionInterval)seq2)); m_kathia.runAction(CCRepeatForever.actionWithAction((CCActionInterval)seq3));