最近的遊戲學習中使用到精靈運動的加速與減速,於是徵集了一些權威的文章供參考。
文章一:
cocos2d-x動畫加速與減速
動畫是遊戲的必然要素之一,在整個遊戲過程中,又有着加速、減速動畫的需求。以塔防爲例子,布塔的時候希望能夠將遊戲減速,布好塔後,則希 望能將遊戲加速;當某個怪被冰凍後,移動速度減緩,而其他怪的移動速度不變。cocos2d-x引擎爲我們提供了很強大的接口,下面就將我實驗的過程複述 一遍,也方便他人。
1)實現全局的加速、減速。
通過設置Scheduler的timeScale,可以實現全局的加、減速。代碼非常簡單:
CCScheduler* pScheduler = CCDirector::sharedDirector()->getScheduler();
pScheduler->setTimeScale(2.0f); //實現加速效果
pScheduler->setTimeScale(0.5f);//實現減速效果
2)實現對某個CCActionInterval動作的加速、減速
方法一:很容易想到的一個方法就是改變CCAnimation的delay unit。代碼如下:
CCAnimation* pAnimation = CCAnimationCache::sharedAnimationCache()->animationByName(“xxx”);
pAnimation->setDelayUnit(pAnimation->getDelayUnit()*0.2f); //速度爲原來的5倍
這個方法有一個缺點:改變了CCAnimationCache中這個animation的delay unit。也就是說以後即使再從CCAnimationCache中獲取這個animation,其delay unit已經是原來的0.2倍了。
方法二:cocos2d-x提供了CCSpeed的類,可以實現動畫速度的調節。用法如下:
CCActionInterval* pActionInterval = CCMoveTo::create(5.0f, ccp(500.0f, 100.0f));
CCSpeed* pSpeed= CCSpeed::create(pActionInterval, 1.5f); //1.5倍速運行
CCSpeed* pSpeed1 = CCSpeed::create(pActionInterval, 0.2f);// 0.2倍速運行
pSprite->runAction(pSpeed);
注意,如果pSprite有已經運行的動作,要用pSprite->stopActionByTag()停掉之前的動作,不然兩個動作就疊加到一起了。
—————————————————————–華麗麗的分割線————————————————————————–
來自HIMI的提示:
很多時候你的主角的動作利用CCAction來實現,移動則是在update刷幀函 數或者一些選擇器的方法中進行的,那麼爲了讓你的主角慢動作比較逼真,那麼Himi建議不要使用scheduleUpdate函數,因爲這個你無法修改每 次調用update的時間默認都是每幀都調用,那麼你應該自己定義一個選擇器當刷邏輯的函數,這樣就能配合CCSpeed實現逼真慢動作拉~
3)對某個CCFiniteTimeAction類型動作的加速、減速
大部分時候,一個遊戲人物的動作並非由單一一個CCActionInterval類型的動作構成,而是一串動作連起來,構成一個Sequence。 用CCSequence::create(…)創建的對象都是CCFinteTimeAction類型的,CCSpeed並不適用。在CCSpeed類的 說明裏,明確指 出”This action can’t be Sequenceable because it is not an CCIntervalAction”。 那對於Sequence就束手無策了嗎?非也。cocos2d-x引擎自帶例子中,schedulerTest給我們展示瞭如何控制某個sprite的 scheduler的timescale。廢話少說,直接看代碼。
在class TwoSchedulers中定義了兩個customer的scheduler和兩個CCActionManager。
CCScheduler *sched1;
CCScheduler *sched2;
CCActionManager *actionManager1;
CCActionManager *actionManager2;
在onEnter函數中,分別對兩個sprite設置customer的ActionManager.
CCScheduler *defaultScheduler = CCDirector::sharedDirector()->getScheduler();
// Create a new scheduler, and link it to the main scheduler
sched1 = new CCScheduler();
defaultScheduler->scheduleUpdateForTarget(sched1, 0, false);
// Create a new ActionManager, and link it to the new scheudler
actionManager1 = new CCActionManager();
sched1->scheduleUpdateForTarget(actionManager1, 0, false);
// Replace the default ActionManager with the new one.
pSprite1->setActionManager(actionManager1);
通過以上的代碼,就可以通過改變sched1的timescale來改變pSprite1的動作的快慢了。有了這種方法,那麼就可以放棄CCSpeed的那種方法了。
文章二:
在實現運動中,我們常常需要實現一些加速度或者減速度的效果,cocos2d-x引擎爲我們提供了相應的實現接口,這樣我們就不用再用原來的公式計算方法來實現加減速度的效果
Ease系列的方法改變了運動的速度,但是並沒有改變總體時間,如果整個的action持續5秒鐘,那麼整個的時間仍然會持續5秒鐘。
這些action可以被分成3類:
In actions: action開始的時候加速
Out actions: action結束的時候加速
InOut actions: action開始,結束的時候加速
第一個參數爲要加減速度的動作,第二個爲加減的速率
還有一些特殊的緩衝公式繼承了進來
1.指數緩衝
EaseExponentialIn
EaseExponentialOut
EaseExponentialInOut
2.賽因緩衝
EaseSineIn
EaseSineOut
EaseSineInOut
3.彈性緩衝
EaseElasticIn
EaseElasticOut
EaseElasticInOut
4.跳躍緩衝
EaseBounceIn
EaseBounceOut
EaseBounceInOut
5.回震緩衝
EaseBackIn
EaseBackOut
EaseBackInOut
另外還可以設置速度的倍數
通過把動作定義爲CCSpeed並改變速度,使用setSpeed將速度按參數的倍數變大或者縮小,這樣可以手動實現加減速度
文章三:移動開發:Cocos2d-x 2.0變速動畫深入分析
變 速動畫都是由時間動畫所衍生出來的,它是通過對一個勻速動畫的進度進行調節來實現的,所以你要運行一個變速動畫,首先要給他指定一個勻速動畫,播放這個變 速動畫,它也播放被指定的勻速動畫,在變速動畫的更新函數中對被指定的勻速動畫通過一個變速曲線計算得到相應的播放進度,變速動畫停止播放時,被指定的勻 速動畫也必須被停止。所以,變速動畫其實就是一個控制器,用來控制一個勻速動畫的播放進度,使其產生變速的效果。
爲了講述好本節,我專門寫了一個曲線生成工具,這樣可以更直觀的看到變速曲線的形態。有興趣的可以到我的羣裏下載這個工具。
打開CActionEase.h:
[cpp] view plaincopy
- #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:
[cpp] view plaincopy
- //靜態函數:創建對應勻速動畫的變速動畫,參數爲一個勻速動畫。內部調用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());
- }
上面只是一個基類,它並未真正的提供速度的變化調節,下面還有一個基類,提供了一個速度調節係數值。
[cpp] view plaincopy
- //可以設定速度的變速動畫基類。
- 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實現:
[cpp] view plaincopy
- //靜態函數:創建對應勻速動畫的變速動畫,參數一爲一個勻速動畫。參數二爲速度。內部調用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);
- }
第二個類有了速度屬性,但這個速度屬性並未對動畫起任何作用。後面的類由這個帶速度屬性的動畫基類派生,真正實現相應的變速效果。
[cpp] view plaincopy
- //由快變慢的變速動畫。
- 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:
[cpp] view plaincopy
- //靜態函數:創建對應勻速動畫的變速動畫,參數一爲一個勻速動畫。參數二爲速度。內部調用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);
- }
[cpp] view plaincopy
- //由慢變快的變速動畫,與上個類基個相同,只是在更新函數中速度值不同。
- 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:
[cpp] view plaincopy
- //靜態函數:創建對應勻速動畫的變速動畫,參數一爲一個勻速動畫。參數二爲速度。內部調用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);
- }
[cpp] view plaincopy
- 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);
- }
[cpp] view plaincopy
- 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());
- }
[cpp] view plaincopy
- 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());
- }
[cpp] view plaincopy
- 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());
- }
[cpp] view plaincopy
- //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());
- }
[cpp] view plaincopy
- //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());
- }
[cpp] view plaincopy
- //另種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());
- }
[cpp] view plaincopy
- //一個基類,用於衍生後面的變速動畫。
- 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;
- }
[cpp] view plaincopy
- //上面基類衍生的新類。
- 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);
- }
[cpp] view plaincopy
- 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);
- }
[cpp] view plaincopy
- 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);
- }
[cpp] view plaincopy
- //又是一個變速動畫的基類,用於衍生一些複雜的變速曲線。
- 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());
- }
[cpp] view plaincopy
- //上面類的衍生類。
- 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());
- }
[cpp] view plaincopy
- 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());
- }
[cpp] view plaincopy
- 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());
- }
[cpp] view plaincopy
- 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());
- }
[cpp] view plaincopy
- 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());
- }
[cpp] view plaincopy
- 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());
- }