本章为我的Cocos2d-x教程一书初稿。望各位看官多提建议!

Cocos2d-x 2.0变速动画深入分析

另:本章所用Cocos2d-x版本为:

​cocos2d-2.0-x-2.0.2 @ Aug 30 2012​

​http://cn.cocos2d-x.org/download​

       变速动画都是由时间动画所衍生出来的,它是通过对一个匀速动画的进度进行调节来实现的,所以你要运行一个变速动画,首先要给他指定一个匀速动画,播放这个变速动画,它也播放被指定的匀速动画,在变速动画的更新函数中对被指定的匀速动画通过一个变速曲线计算得到相应的播放进度,变速动画停止播放时,被指定的匀速动画也必须被停止。所以,变速动画其实就是一个控制器,用来控制一个匀速动画的播放进度,使其产生变速的效果。

      为了讲述好本节,我专门写了一个曲线生成工具,这样可以更直观的看到变速曲线的形态。有兴趣的可以到我的群里下载这个工具。

      打开CActionEase.h:



#ifndef __ACTION_CCEASE_ACTION_H__
#define __ACTION_CCEASE_ACTION_H__

#include "CCActionInterval.h"
//使用Cocos2d命名空间
NS_CC_BEGIN
//要用到以下两个类
class CCObject;
class CCZone;

//所有变速动画的基类。
class CC_DLL CCActionEase : public CCActionInterval
{
public:
//析构
virtual ~CCActionEase(void);

//初始化动画,参数为一个匀速时间动画。
bool initWithAction(CCActionInterval *pAction);
//产生一个当前实例的拷贝。
  virtual CCObject* copyWithZone(CCZone* pZone);
  //指定演示当前动画的演员。
  virtual void startWithTarget(CCNode *pTarget);
  //停止当前动画。
  virtual void stop(void);
  //更新动画。
  virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);

public:
//静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCActionEase* actionWithAction(CCActionInterval *pAction);

//同上
static CCActionEase* create(CCActionInterval *pAction);

protected:
//保存对应的匀速动画。
CCActionInterval *m_pOther;
};


对应CPP:




//静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CCActionEase* CCActionEase::actionWithAction(CCActionInterval *pAction)
{
return CCActionEase::create(pAction);
}
//同上
CCActionEase* CCActionEase::create(CCActionInterval *pAction)
{
  //使用new创建一个当前类实例.
CCActionEase *pRet = new CCActionEase();
if (pRet)
   {
     //初始化.
if (pRet->initWithAction(pAction))
{
   //交由内存管理器进行释放管理.
pRet->autorelease();
}
else
{
   //如果初始化失败,则直接释放
CC_SAFE_RELEASE_NULL(pRet);
}
}
  //返回创建的实例.
return pRet;
}
//初始化.
bool CCActionEase::initWithAction(CCActionInterval *pAction)
{
  //有效性判断.
CCAssert(pAction != NULL, "");
  //先调用基类的初始化函数.
if (CCActionInterval::initWithDuration(pAction->getDuration()))
   {
     //如果成功保存参数动画,本着"占用就加1"的原则,对其引用计数加一.
m_pOther = pAction;
pAction->retain();

return true;
}

return false;
}
//产生一个当前类的实例拷贝.
CCObject* CCActionEase::copyWithZone(CCZone *pZone)
{
  //
CCZone* pNewZone = NULL;
CCActionEase* pCopy = NULL;
//判断参数有效以及其内部已经有创建好的拷贝。
  if(pZone && pZone->m_pCopyObject)
{
//直接强转后返回这个创建好的拷贝。
pCopy = (CCActionEase*)(pZone->m_pCopyObject);
}
else
   {
     //如果无效,新创建一个当前类实例,并创建一个用于拷贝的类实例,将当前类实例设为拷贝类实例的内部拷贝,其实就是建立一个通用的拷贝对象,它内部有一个万物基类CCObject的指针,用来保存各个派生类的实例对象。
pCopy = new CCActionEase();
pZone = pNewZone = new CCZone(pCopy);
}
  //先调用基类的相应函数对其进行基类属性的相关初始化,这个函数会一层层调用当前类基类直至CCAction的相应函数。
CCActionInterval::copyWithZone(pZone);
  //使用保存的匀速动画来初始化拷贝实例。
pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));
//释放通用的拷贝对象。
CC_SAFE_DELETE(pNewZone);
return pCopy;
}
//析构
CCActionEase::~CCActionEase(void)
{
  //释放占用的匀速动画实例。
CC_SAFE_RELEASE(m_pOther);
}

//指定演示当前动画的演员。
void CCActionEase::startWithTarget(CCNode *pTarget)
{
  //调用基类的相应函数。
   CCActionInterval::startWithTarget(pTarget);
   //设定匀速动画的演员。
m_pOther->startWithTarget(m_pTarget);
}
//停止当前动画。
void CCActionEase::stop(void)
{
  //让匀速动画停止播放。
   m_pOther->stop();
   //调用基类的相应函数停止当前动画。
CCActionInterval::stop();
}
//更新动画。
void CCActionEase::update(float time)
{
  //更新匀速动画。
m_pOther->update(time);
}
//创建一个反向播放的变速动画。
CCActionInterval* CCActionEase::reverse(void)
{
  //通过创建一个反向播放的匀速动画做为参数来创建相应的变速动画。
return CCActionEase::create(m_pOther->reverse());
}


       上面只是一个基类,它并未真正的提供速度的变化调节,下面还有一个基类,提供了一个速度调节系数值。

//可以设定速度的变速动画基类。
class CC_DLL CCEaseRateAction : public CCActionEase
{
public:
//析构函数。
virtual ~CCEaseRateAction(void);

//设置速度调节系数。
inline void setRate(float rate) { m_fRate = rate; }
//取得速度调节系数。
inline float getRate(void) { return m_fRate; }

//初始化当前动画。
bool initWithAction(CCActionInterval *pAction, float fRate);
//创建一个当前动画的实例拷贝。
   virtual CCObject* copyWithZone(CCZone* pZone);
   //创建一个反向播放的当前动画。
virtual CCActionInterval* reverse(void);

public:
//静态函数:创建对应匀速动画的变速动画,参数一为一个匀速动画。参数二为速度。内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseRateAction* actionWithAction(CCActionInterval* pAction, float fRate);

//同上。
static CCEaseRateAction* create(CCActionInterval* pAction, float fRate);

protected:
//保存速度调节系数值。
float m_fRate;
};


CPP实现:



//静态函数:创建对应匀速动画的变速动画,参数一为一个匀速动画。参数二为速度。内部调用create实现。
CCEaseRateAction* CCEaseRateAction::actionWithAction(CCActionInterval *pAction, float fRate)
{
return CCEaseRateAction::create(pAction, fRate);
}
//同上。
CCEaseRateAction* CCEaseRateAction::create(CCActionInterval *pAction, float fRate)
{
  //先创建相应的变速动画。
CCEaseRateAction *pRet = new CCEaseRateAction();
if (pRet)
   {
     //如果成功,进行初始化后交由内存管理器处理。
if (pRet->initWithAction(pAction, fRate))
{
pRet->autorelease();
}
else
{
  //如果失败,释放并置空。
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}

//初始化函数。
bool CCEaseRateAction::initWithAction(CCActionInterval *pAction, float fRate)
{
  //调用基类的初始化处理。
if (CCActionEase::initWithAction(pAction))
   {
     //保存速度。
m_fRate = fRate;
return true;
}

return false;
}

//产生一个当前类的实例拷贝。参见基类的解释。
CCObject* CCEaseRateAction::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseRateAction* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseRateAction*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseRateAction();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval*)(m_pOther->copy()->autorelease()), m_fRate);

CC_SAFE_DELETE(pNewZone);
return pCopy;
}

//析构
CCEaseRateAction::~CCEaseRateAction(void)
{
}
//创建一个反向播放的变速动画。
CCActionInterval* CCEaseRateAction::reverse(void)
{
return CCEaseRateAction::create(m_pOther->reverse(), 1 / m_fRate);
}


       第二个类有了速度属性,但这个速度属性并未对动画起任何作用。后面的类由这个带速度属性的动画基类派生,真正实现相应的变速效果。



//由快变慢的变速动画。
class CC_DLL CCEaseIn : public CCEaseRateAction
{
public:
//更新动画。
   virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);
public:
   //静态函数:创建对应匀速动画的变速动画,参数一为一个匀速动画。参数二为速度。内部调用create实现。
    CC_DEPRECATED_ATTRIBUTE static CCEaseIn* actionWithAction(CCActionInterval* pAction, float fRate);

//同上。
static CCEaseIn* create(CCActionInterval* pAction, float fRate);
};


CPP:




//静态函数:创建对应匀速动画的变速动画,参数一为一个匀速动画。参数二为速度。内部调用create实现。
CCEaseIn* CCEaseIn::actionWithAction(CCActionInterval *pAction, float fRate)
{
return CCEaseIn::create(pAction, fRate);
}
//同上,参见CCEaseRateAction的create函数。
CCEaseIn* CCEaseIn::create(CCActionInterval *pAction, float fRate)
{
CCEaseIn *pRet = new CCEaseIn();
if (pRet)
{
if (pRet->initWithAction(pAction, fRate))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//产生一个当前类的实例的拷贝,参见CCEaseRateAction的相应函数。
CCObject* CCEaseIn::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseIn* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseIn*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseIn();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval*)(m_pOther->copy()->autorelease()), m_fRate);

CC_SAFE_DELETE(pNewZone);
return pCopy;
}
//更新动画。
void CCEaseIn::update(float time)
{
  //这里使用了一个浮点的m_fRate次方计算,time值在0~1间变化,但使用powf(time,m_fRate)会在第一象限生成一个曲线。
   m_pOther->update(powf(time, m_fRate));
   //后面的图把m_fRate在0.1到10之间的曲线表现出来,其中X方向代表的是time,也就是进度,系数值就是m_fRate,红色线代表了powf(time,m_fRate)函数。大家可以看到,在m_fRate小于1时函数是先很短时间内达到接近1之后的增速越来越慢,大于1时函数是开始基本都不递增,到后面增速加快,最后非常快。
}

//创建一个反向播放的变速动画。
CCActionInterval* CCEaseIn::reverse(void)
{
return CCEaseIn::create(m_pOther->reverse(),m_fRate);
}

Cocos2d-x 2.0 变速动画深入分析_2d




//由慢变快的变速动画,与上个类基个相同,只是在更新函数中速度值不同。
class CC_DLL CCEaseOut : public CCEaseRateAction
{
public:
//更新动画。
   virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);

public:
   //静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseOut* actionWithAction(CCActionInterval* pAction, float fRate);

//同上。
static CCEaseOut* create(CCActionInterval* pAction, float fRate);
};


CPP:

//静态函数:创建对应匀速动画的变速动画,参数一为一个匀速动画。参数二为速度。内部调用create实现。
CCEaseOut* CCEaseOut::actionWithAction(CCActionInterval *pAction, float fRate)
{
return CCEaseOut::create(pAction, fRate);
}
//同上。
CCEaseOut* CCEaseOut::create(CCActionInterval *pAction, float fRate)
{
CCEaseOut *pRet = new CCEaseOut();
if (pRet)
{
if (pRet->initWithAction(pAction, fRate))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//产生一个当前类的实例拷贝
CCObject* CCEaseOut::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseOut* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseOut*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseOut();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval*)(m_pOther->copy()->autorelease()), m_fRate);

CC_SAFE_DELETE(pNewZone);
return pCopy;
}
//更新函数。
void CCEaseOut::update(float time)
{
  //本动画与上面的动画相似。我也给出曲线图:
m_pOther->update(powf(time, 1 / m_fRate));
}
//创建一个反向播放的变速动画。
CCActionInterval* CCEaseOut::reverse()
{
return CCEaseOut::create(m_pOther->reverse(), 1 / m_fRate);
}


Cocos2d-x 2.0 变速动画深入分析_2d_02


class CC_DLL CCEaseInOut : public CCEaseRateAction
{
public:
//更新动画。
   virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);

public:
  //静态函数:创建对应匀速动画的变速动画,参数一为一个匀速动画。参数二为速度。内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseInOut* actionWithAction(CCActionInterval* pAction, float fRate);

//同上。
static CCEaseInOut* create(CCActionInterval* pAction, float fRate);
};
//静态函数:创建对应匀速动画的变速动画,参数一为一个匀速动画。参数二为速度。内部调用create实现。
CCEaseInOut* CCEaseInOut::actionWithAction(CCActionInterval *pAction, float fRate)
{
return CCEaseInOut::create(pAction, fRate);
}
//同上
CCEaseInOut* CCEaseInOut::create(CCActionInterval *pAction, float fRate)
{
CCEaseInOut *pRet = new CCEaseInOut();
if (pRet)
{
if (pRet->initWithAction(pAction, fRate))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//创建一个当前类的实例拷贝。
CCObject* CCEaseInOut::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseInOut* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseInOut*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseInOut();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval*)(m_pOther->copy()->autorelease()), m_fRate);

CC_SAFE_DELETE(pNewZone);
return pCopy;
}

//更新动画的函数。
void CCEaseInOut::update(float time)
{
  //这个曲线稍复杂,继续上图
time *= 2;
if (time < 1)
{
m_pOther->update(0.5f * powf(time, m_fRate));
}
else
{
m_pOther->update(1.0f - 0.5f * powf(2-time, m_fRate));
}
}
//创建一个反向播放的变速动画。
CCActionInterval* CCEaseInOut::reverse(void)
{
return CCEaseInOut::create(m_pOther->reverse(), m_fRate);
}


Cocos2d-x 2.0 变速动画深入分析_2d_03


class CC_DLL CCEaseExponentialIn : public CCActionEase
{
public:
//更新动画。
   virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);

public:
    //静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseExponentialIn* actionWithAction(CCActionInterval* pAction);
//同上。
static CCEaseExponentialIn* create(CCActionInterval* pAction);
};
//静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CCEaseExponentialIn* CCEaseExponentialIn::actionWithAction(CCActionInterval* pAction)
{
return CCEaseExponentialIn::create(pAction);
}
//同上。
CCEaseExponentialIn* CCEaseExponentialIn::create(CCActionInterval* pAction)
{
CCEaseExponentialIn *pRet = new CCEaseExponentialIn();
if (pRet)
{
if (pRet->initWithAction(pAction))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//创建一个当前类的实例拷贝。
CCObject* CCEaseExponentialIn::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseExponentialIn* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseExponentialIn*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseExponentialIn();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

CC_SAFE_DELETE(pNewZone);
return pCopy;
}

//更新动画。
void CCEaseExponentialIn::update(float time)
{
  //不废话,上曲线,此曲线没有系数。是固定曲线。
m_pOther->update(time == 0 ? 0 : powf(2, 10 * (time/1 - 1)) - 1 * 0.001f);
}

//创建一个反向播放的变速动画。
CCActionInterval* CCEaseExponentialIn::reverse(void)
{
return CCEaseExponentialOut::create(m_pOther->reverse());
}


Cocos2d-x 2.0 变速动画深入分析_初始化_04



class CC_DLL CCEaseExponentialOut : public CCActionEase
{
public:
//更新动画。
   virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);

public:
   //静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseExponentialOut* actionWithAction(CCActionInterval* pAction);
//同上。
static CCEaseExponentialOut* create(CCActionInterval* pAction);
};
//静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CCEaseExponentialOut* CCEaseExponentialOut::actionWithAction(CCActionInterval* pAction)
{
return CCEaseExponentialOut::create(pAction);
}
//同上。
CCEaseExponentialOut* CCEaseExponentialOut::create(CCActionInterval* pAction)
{
CCEaseExponentialOut *pRet = new CCEaseExponentialOut();
if (pRet)
{
if (pRet->initWithAction(pAction))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//创建一个当前动画的实例拷贝。
CCObject* CCEaseExponentialOut::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseExponentialOut* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseExponentialOut*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseExponentialOut();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

CC_SAFE_DELETE(pNewZone);
return pCopy;
}
//更新函数。
void CCEaseExponentialOut::update(float time)
{
  //上曲线说明,此曲线没有系数。是固定曲线。
  
m_pOther->update(time == 1 ? 1 : (-powf(2, -10 * time / 1) + 1));
}
//创建一个反向播放的当前动画。
CCActionInterval* CCEaseExponentialOut::reverse(void)
{
return CCEaseExponentialIn::create(m_pOther->reverse());
}


Cocos2d-x 2.0 变速动画深入分析_静态函数_05



class CC_DLL CCEaseExponentialInOut : public CCActionEase
{
public:
//更新动画。
   virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);
public:
   //静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseExponentialInOut* actionWithAction(CCActionInterval* pAction);

   //同上。
static CCEaseExponentialInOut* create(CCActionInterval* pAction);
};
//静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CCEaseExponentialInOut* CCEaseExponentialInOut::actionWithAction(CCActionInterval *pAction)
{
return CCEaseExponentialInOut::create(pAction);
}
//同上。
CCEaseExponentialInOut* CCEaseExponentialInOut::create(CCActionInterval *pAction)
{
CCEaseExponentialInOut *pRet = new CCEaseExponentialInOut();
if (pRet)
{
if (pRet->initWithAction(pAction))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//创建一个当前动画的实例拷贝。
CCObject* CCEaseExponentialInOut::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseExponentialInOut* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseExponentialInOut*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseExponentialInOut();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

CC_SAFE_DELETE(pNewZone);
return pCopy;
}
//动画更新。
void CCEaseExponentialInOut::update(float time)
{
  //上曲线,没有系数,固定曲线
  
time /= 0.5f;
if (time < 1)
{
time = 0.5f * powf(2, 10 * (time - 1));
}
else
{
time = 0.5f * (-powf(2, -10 * (time - 1)) + 2);
}

m_pOther->update(time);
}
//创建一个反向播放的当前动画。
CCActionInterval* CCEaseExponentialInOut::reverse()
{
return CCEaseExponentialInOut::create(m_pOther->reverse());
}


Cocos2d-x 2.0 变速动画深入分析_静态函数_06


//cos曲线方式变化的变速动画。
class CC_DLL CCEaseSineIn : public CCActionEase
{
public:
//更新动画。
   virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);

public:
   //静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseSineIn* actionWithAction(CCActionInterval* pAction);
//同上
static CCEaseSineIn* create(CCActionInterval* pAction);
};
//静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CCEaseSineIn* CCEaseSineIn::actionWithAction(CCActionInterval* pAction)
{
return CCEaseSineIn::create(pAction);
}
//同上
CCEaseSineIn* CCEaseSineIn::create(CCActionInterval* pAction)
{
CCEaseSineIn *pRet = new CCEaseSineIn();
if (pRet)
{
if (pRet->initWithAction(pAction))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//创建一个当前动画的实例拷贝。
CCObject* CCEaseSineIn::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseSineIn* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseSineIn*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseSineIn();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

CC_SAFE_DELETE(pNewZone);
return pCopy;
}
//更新
void CCEaseSineIn::update(float time)
{ //上曲线图,无需参数,固定曲线:
  
m_pOther->update(-1 * cosf(time * (float)M_PI_2) + 1);
}
//创建一个反向播放的当前动画。
CCActionInterval* CCEaseSineIn::reverse(void)
{
return CCEaseSineOut::create(m_pOther->reverse());
}


Cocos2d-x 2.0 变速动画深入分析_2d_07



//sin曲线方式变化的变速动画。
class CC_DLL CCEaseSineOut : public CCActionEase
{
public:
//更新动画。
   virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);

public:
   //静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseSineOut* actionWithAction(CCActionInterval* pAction);
//同上
static CCEaseSineOut* create(CCActionInterval* pAction);
};
//静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CCEaseSineOut* CCEaseSineOut::actionWithAction(CCActionInterval* pAction)
{
return CCEaseSineOut::create(pAction);
}
//同上
CCEaseSineOut* CCEaseSineOut::create(CCActionInterval* pAction)
{
CCEaseSineOut *pRet = new CCEaseSineOut();
if (pRet)
{
if (pRet->initWithAction(pAction))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//创建一个当前动画的实例拷贝。
CCObject* CCEaseSineOut::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseSineOut* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseSineOut*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseSineOut();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

CC_SAFE_DELETE(pNewZone);
return pCopy;
}
//更新
void CCEaseSineOut::update(float time)
{
  //上图说明:
  
m_pOther->update(sinf(time * (float)M_PI_2));
}
//创建一个反向播放的当前动画。
CCActionInterval* CCEaseSineOut::reverse(void)
{
return CCEaseSineIn::create(m_pOther->reverse());
}


Cocos2d-x 2.0 变速动画深入分析_初始化_08



//另种cos曲线方式变化的变速动画。
class CC_DLL CCEaseSineInOut : public CCActionEase
{
public:
//更新动画。
   virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);

public:
   //静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseSineInOut* actionWithAction(CCActionInterval* pAction);
   //同上
static CCEaseSineInOut* create(CCActionInterval* pAction);
};

//静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CCEaseSineInOut* CCEaseSineInOut::actionWithAction(CCActionInterval* pAction)
{
return CCEaseSineInOut::create(pAction);
}
//同上
CCEaseSineInOut* CCEaseSineInOut::create(CCActionInterval* pAction)
{
CCEaseSineInOut *pRet = new CCEaseSineInOut();
if (pRet)
{
if (pRet->initWithAction(pAction))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//创建一个当前动画的实例拷贝。
CCObject* CCEaseSineInOut::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseSineInOut* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseSineInOut*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseSineInOut();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

CC_SAFE_DELETE(pNewZone);
return pCopy;
}
//更新函数。
void CCEaseSineInOut::update(float time)
{
  //上曲线说明:
m_pOther->update(-0.5f * (cosf((float)M_PI * time) - 1));
}

//创建一个反向播放的当前动画。
CCActionInterval* CCEaseSineInOut::reverse()
{
return CCEaseSineInOut::create(m_pOther->reverse());
}


Cocos2d-x 2.0 变速动画深入分析_初始化_09



//一个基类,用于衍生后面的变速动画。
class CC_DLL CCEaseElastic : public CCActionEase
{
public:
//取得系数
inline float getPeriod(void) { return m_fPeriod; }
   //设置系数
inline void setPeriod(float fPeriod) { m_fPeriod = fPeriod; }

//初始化函数。
bool initWithAction(CCActionInterval *pAction, float fPeriod = 0.3f);
  //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   
  //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);

public:
//静态函数:创建对应匀速动画的变速动画,参数一为一个匀速动画。参数二为系数。内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseElastic* actionWithAction(CCActionInterval *pAction, float fPeriod = 0.3f);
//同上
static CCEaseElastic* create(CCActionInterval *pAction, float fPeriod = 0.3f);
protected:
   //曲线系数。
float m_fPeriod;
};

//静态函数:创建对应匀速动画的变速动画,参数一为一个匀速动画。参数二为系数。内部调用create实现。
CCEaseElastic* CCEaseElastic::actionWithAction(CCActionInterval *pAction, float fPeriod/* = 0.3f*/)
{
return CCEaseElastic::create(pAction, fPeriod);
}
//同上
CCEaseElastic* CCEaseElastic::create(CCActionInterval *pAction, float fPeriod/* = 0.3f*/)
{
CCEaseElastic *pRet = new CCEaseElastic();
if (pRet)
{
if (pRet->initWithAction(pAction, fPeriod))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//初始化函数。
bool CCEaseElastic::initWithAction(CCActionInterval *pAction, float fPeriod/* = 0.3f*/)
{
if (CCActionEase::initWithAction(pAction))
   {
     //保存曲线系数。
m_fPeriod = fPeriod;
return true;
}

return false;
}
  
//创建一个当前动画的实例拷贝。
CCObject* CCEaseElastic::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseElastic* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseElastic*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseElastic();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()), m_fPeriod);

CC_SAFE_DELETE(pNewZone);
return pCopy;
}
//创建一个反向播放的当前动画。
CCActionInterval* CCEaseElastic::reverse(void)
{
CCAssert(0, "Override me");

return NULL;
}


//上面基类衍生的新类。
class CC_DLL CCEaseElasticIn : public CCEaseElastic
{
public:
//更新动画。
   virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);

public:
   //静态函数:创建对应匀速动画的变速动画,第一参数为一个匀速动画。第二参数为曲线系数,内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseElasticIn* actionWithAction(CCActionInterval *pAction, float fPeriod = 0.3f);
//同上。
static CCEaseElasticIn* create(CCActionInterval *pAction, float fPeriod = 0.3f);
};

//静态函数:创建对应匀速动画的变速动画,第一参数为一个匀速动画。第二参数为曲线系数,内部调用create实现。
CCEaseElasticIn* CCEaseElasticIn::actionWithAction(CCActionInterval *pAction, float fPeriod/* = 0.3f*/)
{
return CCEaseElasticIn::create(pAction, fPeriod);
}
//同上。
CCEaseElasticIn* CCEaseElasticIn::create(CCActionInterval *pAction, float fPeriod/* = 0.3f*/)
{
CCEaseElasticIn *pRet = new CCEaseElasticIn();
if (pRet)
{
if (pRet->initWithAction(pAction, fPeriod))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//创建一个当前动画的实例拷贝。
CCObject* CCEaseElasticIn::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseElasticIn* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseElasticIn*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseElasticIn();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()), m_fPeriod);

CC_SAFE_DELETE(pNewZone);
return pCopy;
}
//更新动画。
void CCEaseElasticIn::update(float time)
{
  //比较复杂,上图说明在不同的系数时的曲线结果:
  
float newT = 0;
if (time == 0 || time == 1)
{
newT = time;
}
else
{
float s = m_fPeriod / 4;
time = time - 1;
newT = -powf(2, 10 * time) * sinf((time - s) * M_PI_X_2 / m_fPeriod);
}

m_pOther->update(newT);
}
//创建一个反向播放的当前动画。
CCActionInterval* CCEaseElasticIn::reverse(void)
{
return CCEaseElasticOut::create(m_pOther->reverse(), m_fPeriod);
}

Cocos2d-x 2.0 变速动画深入分析_2d_10



class CC_DLL CCEaseElasticOut : public CCEaseElastic
{
public:
//更新动画。
   virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);

public:
   //静态函数:创建对应匀速动画的变速动画,第一参数为一个匀速动画。第二参数为曲线系数,内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseElasticOut* actionWithAction(CCActionInterval *pAction, float fPeriod = 0.3f);

//同上
static CCEaseElasticOut* create(CCActionInterval *pAction, float fPeriod = 0.3f);
};
   //静态函数:创建对应匀速动画的变速动画,第一参数为一个匀速动画。第二参数为曲线系数,内部调用create实现。

CCEaseElasticOut* CCEaseElasticOut::actionWithAction(CCActionInterval *pAction, float fPeriod/* = 0.3f*/)
{
return CCEaseElasticOut::create(pAction, fPeriod);
}
//同上
CCEaseElasticOut* CCEaseElasticOut::create(CCActionInterval *pAction, float fPeriod/* = 0.3f*/)
{
CCEaseElasticOut *pRet = new CCEaseElasticOut();
if (pRet)
{
if (pRet->initWithAction(pAction, fPeriod))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//创建一个当前动画的实例拷贝。
CCObject *CCEaseElasticOut::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseElasticOut* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseElasticOut*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseElasticOut();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()), m_fPeriod);

CC_SAFE_DELETE(pNewZone);
return pCopy;
}
//更新动画。
void CCEaseElasticOut::update(float time)
{
  //继续上曲线说明:
  
float newT = 0;
if (time == 0 || time == 1)
{
newT = time;
}
else
{
float s = m_fPeriod / 4;
newT = powf(2, -10 * time) * sinf((time - s) * M_PI_X_2 / m_fPeriod) + 1;
}

m_pOther->update(newT);
}
//创建一个反向播放的当前动画。
CCActionInterval* CCEaseElasticOut::reverse(void)
{
return CCEaseElasticIn::create(m_pOther->reverse(), m_fPeriod);
}


Cocos2d-x 2.0 变速动画深入分析_静态函数_11



class CC_DLL CCEaseElasticInOut : public CCEaseElastic
{
public:
//更新动画。
   virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);

public:
   //静态函数:创建对应匀速动画的变速动画,第一参数为一个匀速动画。第二参数为曲线系数,内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseElasticInOut* actionWithAction(CCActionInterval *pAction, float fPeriod = 0.3f);

//同上
static CCEaseElasticInOut* create(CCActionInterval *pAction, float fPeriod = 0.3f);
};
//静态函数:创建对应匀速动画的变速动画,第一参数为一个匀速动画。第二参数为曲线系数,内部调用create实现。
CCEaseElasticInOut* CCEaseElasticInOut::actionWithAction(CCActionInterval *pAction, float fPeriod/* = 0.3f*/)
{
return CCEaseElasticInOut::create(pAction, fPeriod);
}
//同上
CCEaseElasticInOut* CCEaseElasticInOut::create(CCActionInterval *pAction, float fPeriod/* = 0.3f*/)
{
CCEaseElasticInOut *pRet = new CCEaseElasticInOut();
if (pRet)
{
if (pRet->initWithAction(pAction, fPeriod))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//创建一个当前动画的实例拷贝。
CCObject* CCEaseElasticInOut::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseElasticInOut* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseElasticInOut*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseElasticInOut();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()), m_fPeriod);

CC_SAFE_DELETE(pNewZone);
return pCopy;

}
//更新动画。
void CCEaseElasticInOut::update(float time)
{
  //挺复杂的曲线:
  
float newT = 0;
if (time == 0 || time == 1)
{
newT = time;
}
else
{
time = time * 2;
if (! m_fPeriod)
{
m_fPeriod = 0.3f * 1.5f;
}

float s = m_fPeriod / 4;

time = time - 1;
if (time < 0)
{
newT = -0.5f * powf(2, 10 * time) * sinf((time -s) * M_PI_X_2 / m_fPeriod);
}
else
{
newT = powf(2, -10 * time) * sinf((time - s) * M_PI_X_2 / m_fPeriod) * 0.5f + 1;
}
}

m_pOther->update(newT);
}
//创建一个反向播放的当前动画。
CCActionInterval* CCEaseElasticInOut::reverse(void)
{
return CCEaseElasticInOut::create(m_pOther->reverse(), m_fPeriod);
}


Cocos2d-x 2.0 变速动画深入分析_2d_12


//又是一个变速动画的基类,用于衍生一些复杂的变速曲线。
class CC_DLL CCEaseBounce : public CCActionEase
{
public:
  //计算曲线处理
   float bounceTime(float time);
   //产生一个当前类的实例拷贝。
   virtual CCObject* copyWithZone(CCZone* pZone);
   //创建一个反向播放的当前动画
virtual CCActionInterval* reverse();

public:
   //静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseBounce* actionWithAction(CCActionInterval* pAction);
//同上。
static CCEaseBounce* create(CCActionInterval* pAction);
};
   
//静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CCEaseBounce* CCEaseBounce::actionWithAction(CCActionInterval* pAction)
{
return CCEaseBounce::create(pAction);
}
//同上。
CCEaseBounce* CCEaseBounce::create(CCActionInterval* pAction)
{
CCEaseBounce *pRet = new CCEaseBounce();
if (pRet)
{
if (pRet->initWithAction(pAction))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//产生一个当前类的实例拷贝。
CCObject* CCEaseBounce::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseBounce* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseBounce*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseBounce();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

CC_SAFE_DELETE(pNewZone);
return pCopy;
}
//计算曲线处理
float CCEaseBounce::bounceTime(float time)
{
  //复杂曲线说明:
  
if (time < 1 / 2.75)
{
return 7.5625f * time * time;
} else
if (time < 2 / 2.75)
{
time -= 1.5f / 2.75f;
return 7.5625f * time * time + 0.75f;
} else
if(time < 2.5 / 2.75)
{
time -= 2.25f / 2.75f;
return 7.5625f * time * time + 0.9375f;
}

time -= 2.625f / 2.75f;
return 7.5625f * time * time + 0.984375f;
}
//创建一个反向播放的当前动画
CCActionInterval* CCEaseBounce::reverse()
{
return CCEaseBounce::create(m_pOther->reverse());
}



//上面类的衍生类。
class CC_DLL CCEaseBounceIn : public CCEaseBounce
{
public:
//更新动画。
   virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);

public:
   //静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseBounceIn* actionWithAction(CCActionInterval* pAction);
//同上。
static CCEaseBounceIn* create(CCActionInterval* pAction);
};
//静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CCEaseBounceIn* CCEaseBounceIn::actionWithAction(CCActionInterval* pAction)
{
return CCEaseBounceIn::create(pAction);
}
//同上。
CCEaseBounceIn* CCEaseBounceIn::create(CCActionInterval* pAction)
{
CCEaseBounceIn *pRet = new CCEaseBounceIn();
if (pRet)
{
if (pRet->initWithAction(pAction))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//创建一个当前动画的实例拷贝。
CCObject* CCEaseBounceIn::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseBounceIn* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseBounceIn*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseBounceIn();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

CC_SAFE_DELETE(pNewZone);
return pCopy;
}
//更新动画。
void CCEaseBounceIn::update(float time)
{
  //先计算出变速曲线的结果然后做为进度值设置给匀速动画。
  //曲线未设系数,为固定曲线。
  
float newT = 1 - bounceTime(1 - time);
m_pOther->update(newT);
}
//创建一个反向播放的当前动画。
CCActionInterval* CCEaseBounceIn::reverse(void)
{
return CCEaseBounceOut::create(m_pOther->reverse());
}


Cocos2d-x 2.0 变速动画深入分析_静态函数_13



class CC_DLL CCEaseBounceOut : public CCEaseBounce
{
public:
//更新动画。
   virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);

public:
   //静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseBounceOut* actionWithAction(CCActionInterval* pAction);
//同上
static CCEaseBounceOut* create(CCActionInterval* pAction);
};
//静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CCEaseBounceOut* CCEaseBounceOut::actionWithAction(CCActionInterval* pAction)
{
return CCEaseBounceOut::create(pAction);
}
//同上
CCEaseBounceOut* CCEaseBounceOut::create(CCActionInterval* pAction)
{
CCEaseBounceOut *pRet = new CCEaseBounceOut();
if (pRet)
{
if (pRet->initWithAction(pAction))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//创建一个当前动画的实例拷贝。
CCObject* CCEaseBounceOut::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseBounceOut* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseBounceOut*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseBounceOut();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

CC_SAFE_DELETE(pNewZone);
return pCopy;
}
//更新动画
void CCEaseBounceOut::update(float time)
{
  //曲线说明:
  //曲线未设系数,为固定曲线。
  
float newT = bounceTime(time);
m_pOther->update(newT);
}
//创建一个反向播放的当前动画。
CCActionInterval* CCEaseBounceOut::reverse(void)
{
return CCEaseBounceIn::create(m_pOther->reverse());
}

Cocos2d-x 2.0 变速动画深入分析_2d_14



class CC_DLL CCEaseBounceInOut : public CCEaseBounce
{
public:
//更新动画。
   virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);

public:
   //静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseBounceInOut* actionWithAction(CCActionInterval* pAction);
//同上
static CCEaseBounceInOut* create(CCActionInterval* pAction);
};
   //静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CCEaseBounceInOut* CCEaseBounceInOut::actionWithAction(CCActionInterval* pAction)
{
return CCEaseBounceInOut::create(pAction);
}
//同上
CCEaseBounceInOut* CCEaseBounceInOut::create(CCActionInterval* pAction)
{
CCEaseBounceInOut *pRet = new CCEaseBounceInOut();
if (pRet)
{
if (pRet->initWithAction(pAction))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//创建一个当前动画的实例拷贝。
CCObject* CCEaseBounceInOut::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseBounceInOut* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseBounceInOut*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseBounceInOut();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

CC_SAFE_DELETE(pNewZone);
return pCopy;
}
//动画更新
void CCEaseBounceInOut::update(float time)
{
  //曲线说明:
  
float newT = 0;
if (time < 0.5f)
{
time = time * 2;
newT = (1 - bounceTime(1 - time)) * 0.5f;
}
else
{
newT = bounceTime(time * 2 - 1) * 0.5f + 0.5f;
}

m_pOther->update(newT);
}
//创建一个反向播放的当前动画。

CCActionInterval* CCEaseBounceInOut::reverse()
{
return CCEaseBounceInOut::create(m_pOther->reverse());
}

Cocos2d-x 2.0 变速动画深入分析_静态函数_15


class CC_DLL CCEaseBackIn : public CCActionEase
{
public:
//更新动画。
   virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);

public:
   //静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseBackIn* actionWithAction(CCActionInterval* pAction);
//同上
static CCEaseBackIn* create(CCActionInterval* pAction);
};
//静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CCEaseBackIn* CCEaseBackIn::actionWithAction(CCActionInterval *pAction)
{
return CCEaseBackIn::create(pAction);
}
//同上
CCEaseBackIn* CCEaseBackIn::create(CCActionInterval *pAction)
{
CCEaseBackIn *pRet = new CCEaseBackIn();
if (pRet)
{
if (pRet->initWithAction(pAction))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//创建一个当前动画的实例拷贝。
CCObject* CCEaseBackIn::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseBackIn* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseBackIn*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseBackIn();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

CC_SAFE_DELETE(pNewZone);
return pCopy;
}
//更新动画.
void CCEaseBackIn::update(float time)
{
  //菜花,上曲线!
  
float overshoot = 1.70158f;
m_pOther->update(time * time * ((overshoot + 1) * time - overshoot));
}
//创建一个反向播放的当前动画。
CCActionInterval* CCEaseBackIn::reverse(void)
{
return CCEaseBackOut::create(m_pOther->reverse());
}

Cocos2d-x 2.0 变速动画深入分析_2d_16




class CC_DLL CCEaseBackOut : public CCActionEase
{
public:
//更新动画。
   virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);

public:
   //静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseBackOut* actionWithAction(CCActionInterval* pAction);
//同上
static CCEaseBackOut* create(CCActionInterval* pAction);
};
//静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CCEaseBackOut* CCEaseBackOut::actionWithAction(CCActionInterval* pAction)
{
return CCEaseBackOut::create(pAction);
}
//同上
CCEaseBackOut* CCEaseBackOut::create(CCActionInterval* pAction)
{
CCEaseBackOut *pRet = new CCEaseBackOut();
if (pRet)
{
if (pRet->initWithAction(pAction))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//创建一个当前动画的实例拷贝。
CCObject* CCEaseBackOut::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseBackOut* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseBackOut*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseBackOut();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

CC_SAFE_DELETE(pNewZone);
return pCopy;
}
//更新动画.
void CCEaseBackOut::update(float time)
{
  //奶妈,上曲线!
  
float overshoot = 1.70158f;

time = time - 1;
m_pOther->update(time * time * ((overshoot + 1) * time + overshoot) + 1);
}
//创建一个反向播放的当前动画。
CCActionInterval* CCEaseBackOut::reverse(void)
{
return CCEaseBackIn::create(m_pOther->reverse());
}


Cocos2d-x 2.0 变速动画深入分析_初始化_17



class CC_DLL CCEaseBackInOut : public CCActionEase
{
public:
//更新动画。
   virtual void update(float time);
   //创建一个反向播放的当前动画。
   virtual CCActionInterval* reverse(void);
   //创建一个当前动画的实例拷贝。
virtual CCObject* copyWithZone(CCZone* pZone);

public:
   //静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CC_DEPRECATED_ATTRIBUTE static CCEaseBackInOut* actionWithAction(CCActionInterval* pAction);
   //同上
static CCEaseBackInOut* create(CCActionInterval* pAction);
};
//静态函数:创建对应匀速动画的变速动画,参数为一个匀速动画。内部调用create实现。
CCEaseBackInOut* CCEaseBackInOut::actionWithAction(CCActionInterval* pAction)
{
return CCEaseBackInOut::create(pAction);
}
//同上
CCEaseBackInOut* CCEaseBackInOut::create(CCActionInterval* pAction)
{
CCEaseBackInOut *pRet = new CCEaseBackInOut();
if (pRet)
{
if (pRet->initWithAction(pAction))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
}

return pRet;
}
//创建一个当前动画的实例拷贝。
CCObject* CCEaseBackInOut::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCEaseBackInOut* pCopy = NULL;
if(pZone && pZone->m_pCopyObject)
{
//in case of being called at sub class
pCopy = (CCEaseBackInOut*)(pZone->m_pCopyObject);
}
else
{
pCopy = new CCEaseBackInOut();
pNewZone = new CCZone(pCopy);
}

pCopy->initWithAction((CCActionInterval *)(m_pOther->copy()->autorelease()));

CC_SAFE_DELETE(pNewZone);
return pCopy;
}
//动画更新
void CCEaseBackInOut::update(float time)
{
  //容嬷嬷,上曲线!
  
float overshoot = 1.70158f * 1.525f;

time = time * 2;
if (time < 1)
{
m_pOther->update((time * time * ((overshoot + 1) * time - overshoot)) / 2);
}
else
{
time = time - 2;
m_pOther->update((time * time * ((overshoot + 1) * time + overshoot)) / 2 + 1);
}
}

//创建一个反向播放的当前动画。
CCActionInterval* CCEaseBackInOut::reverse()
{
return CCEaseBackInOut::create(m_pOther->reverse());
}


Cocos2d-x 2.0 变速动画深入分析_静态函数_18



     虽然大部分代码很无聊,但是我相信,时间久了,各位同学会发现,本博的代码仍是您在进行Cocos2d-x开发的超级帮助。最后,周末愉快!~

                          

Cocos2d-x 2.0 变速动画深入分析_2d_19