生活随笔
收集整理的這篇文章主要介紹了
COCOS2D-X 抖动效果 CCShake
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
cocos2dx全屏抖動,個別對象抖動
[cpp]?view plain
?copy ? ? ? ? ? ? ? ?? #ifndef?__SHAKE_H__?? #define?__SHAKE_H__?? ?? #include?"actions/CCActionInterval.h"?? ?? ? ? ?? class?CCShake?:?public?cocos2d::CCActionInterval?? {?? public:?? ????CCShake();?? ?????? ?????? ????static?CCShake*?create(float?d,?float?strength?);?? ?????? ????static?CCShake*?createWithStrength(float?d,?float?strength_x,?float?strength_y?);?? ????bool?initWithDuration(float?d,?float?strength_x,?float?strength_y?);?? ?????? protected:?? ?????? ????virtual?void?startWithTarget(cocos2d::CCNode?*pTarget);?? ????virtual?void?update(float?time);?? ????virtual?void?stop(void);?? ?????? ?????? ?????? ????float?m_initial_x,?m_initial_y;?? ?????? ????float?m_strength_x,?m_strength_y;?? };?? ?? ?? ? ? ?? class?CCFallOffShake?:?public?CCShake?? {?? public:?? ????CCFallOffShake();?? ?????? ?????? ????static?CCFallOffShake*?create(float?d,?float?strength?);?? ?????? ????static?CCFallOffShake*?createWithStrength(float?d,?float?strength_x,?float?strength_y?);?? ?????? protected:?? ????virtual?void?update(float?time);?? };?? ?? #endif?//__SHAKE_H__??
CPP
[cpp]?view plain
?copy #include?"ShakeAction.h"?? #include?"cocos2d.h"?? USING_NS_CC;?? ?? ?? CCShake::CCShake()?:?m_strength_x(0),?m_strength_y(0),?m_initial_x(0),?m_initial_y(0)?? {?? }?? ?? CCShake*?CCShake::create(?float?d,?float?strength?)?? {?? ?????? ????return?createWithStrength(?d,?strength,?strength?);?? }?? ?? CCShake*?CCShake::createWithStrength(float?duration,?float?strength_x,?float?strength_y)?? {?? ????CCShake?*pRet?=?new?CCShake();?? ?????? ????if?(pRet?&&?pRet->initWithDuration(duration,?strength_x,?strength_y))?? ????{?? ????????pRet->autorelease();?? ????}?? ????else?? ????{?? ????????CC_SAFE_DELETE(pRet);?? ????}?? ?????? ?????? ????return?pRet;?? }?? ?? bool?CCShake::initWithDuration(float?duration,?float?strength_x,?float?strength_y)?? {?? ????if?(CCActionInterval::initWithDuration(duration))?? ????{?? ????????m_strength_x?=?strength_x;?? ????????m_strength_y?=?strength_y;?? ????????return?true;?? ????}?? ?????? ????return?false;?? }?? ?? ?? ?? static?float?fgRangeRand(?float?min,?float?max?)?? {?? ????float?rnd?=?CCRANDOM_0_1();?? ????return?rnd*(max-min)+min;?? }?? ?? void?CCShake::update(float?dt)?? {?? ????float?randx?=?fgRangeRand(?-m_strength_x,?m_strength_x?)*dt;?? ????float?randy?=?fgRangeRand(?-m_strength_y,?m_strength_y?)*dt;?? ?????? ?????? ????m_pTarget->setPosition(?ccpAdd(ccp(m_initial_x,?m_initial_y),ccp(?randx,?randy)));?? }?? ?? void?CCShake::startWithTarget(CCNode?*pTarget)?? {?? ????CCActionInterval::startWithTarget(?pTarget?);?? ?????? ?????? ????m_initial_x?=?pTarget->getPosition().x;?? ????m_initial_y?=?pTarget->getPosition().y;?? }?? ?? void?CCShake::stop(void)?? {?? ?????? ????this->getTarget()->setPosition(?ccp(?m_initial_x,?m_initial_y?)?);?? ?????? ????CCActionInterval::stop();?? }?? ?? ?? ?? ?? CCFallOffShake::CCFallOffShake()?? {?? ?????? }?? ?? ?? CCFallOffShake*?CCFallOffShake::create(float?d,?float?strength?)?? {?? ????return?createWithStrength(?d,?strength,?strength?);?? }?? ?? ?? CCFallOffShake*?CCFallOffShake::createWithStrength(float?duration,?float?strength_x,?float?strength_y)?? {?? ????CCFallOffShake?*pRet?=?new?CCFallOffShake();?? ?????? ????if?(pRet?&&?pRet->initWithDuration(duration,?strength_x,?strength_y))?? ????{?? ????????pRet->autorelease();?? ????}?? ????else?? ????{?? ????????CC_SAFE_DELETE(pRet);?? ????}?? ?????? ????return?pRet;?? }?? ?? void?CCFallOffShake::update(float?dt)?? {?? ????float?rate?=?(getDuration()?-?getElapsed())/getDuration();?? ????if?(rate?<?0.f)?{?? ????????rate?=?0.f;?? ????}?? ?????? ????float?randx?=?fgRangeRand(?-m_strength_x,?m_strength_x?)*rate;?? ????float?randy?=?fgRangeRand(?-m_strength_y,?m_strength_y?)*rate;?? ?????? ?????? ????m_pTarget->setPosition(?ccpAdd(ccp(m_initial_x,?m_initial_y),ccp(?randx,?randy)));?? }??
用法:
pSprite->runAction(CCShake::create(0.1f,10));
?pSprite:想抖動的物體
第一個參數是:抖動的時間
第一個參數是:抖動的幅度
注意
一個CCNode同時執行多個CCShake動作,或者一個CCShake沒有完又執行一個CCShake的話就會出現問題,會出現偏移的現象!
解決方案:
1).不要同時執行多個CCShake動作.
2.自己外部記錄這個CCNode的位置,執行完成后手動setPosition();
轉載于:https://www.cnblogs.com/Anzhongliu/p/6091822.html
總結
以上是生活随笔為你收集整理的COCOS2D-X 抖动效果 CCShake的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。