日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

COCOS2D-X 抖动效果 CCShake

發布時間:2024/4/17 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 COCOS2D-X 抖动效果 CCShake 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

cocos2dx全屏抖動,個別對象抖動


[cpp]?view plain?copy
  • /**?
  • ?desc:讓指定控件抖動?
  • ?一個CCNode同時執行多個CCShake動作,或者一個CCShake沒有完又執行一個CCShake的話就會出現問題,會出現偏移的現象!?
  • ??
  • ?解決方案:?
  • ?1).不要同時執行多個CCShake動作.?
  • ?2.自己外部記錄這個CCNode的位置,執行完成后手動setPosition();?
  • ?*/??
  • #ifndef?__SHAKE_H__??
  • #define?__SHAKE_H__??
  • ??
  • #include?"actions/CCActionInterval.h"??
  • ??
  • /**?
  • ?*?按指定頻度范圍內抖動[-strength_x,strength_x][-strength_y,?strength_y]?
  • ?*/??
  • class?CCShake?:?public?cocos2d::CCActionInterval??
  • {??
  • public:??
  • ????CCShake();??
  • ??????
  • ????//?Create?the?action?with?a?time?and?a?strength?(same?in?x?and?y)??
  • ????static?CCShake*?create(float?d,?float?strength?);??
  • ????//?Create?the?action?with?a?time?and?strengths?(different?in?x?and?y)??
  • ????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);??
  • ??????
  • ??????
  • ????//?Initial?position?of?the?shaked?node??
  • ????float?m_initial_x,?m_initial_y;??
  • ????//?Strength?of?the?action??
  • ????float?m_strength_x,?m_strength_y;??
  • };??
  • ??
  • ??
  • /**?
  • ?*?線性抖動(剩余時間越短,抖動范圍越小)?
  • ?*/??
  • class?CCFallOffShake?:?public?CCShake??
  • {??
  • public:??
  • ????CCFallOffShake();??
  • ??????
  • ????//?Create?the?action?with?a?time?and?a?strength?(same?in?x?and?y)??
  • ????static?CCFallOffShake*?create(float?d,?float?strength?);??
  • ????//?Create?the?action?with?a?time?and?strengths?(different?in?x?and?y)??
  • ????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;??
  • ??
  • //?not?really?useful,?but?I?like?clean?default?constructors??
  • 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?)??
  • {??
  • ????//?call?other?construction?method?with?twice?the?same?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;??
  • }??
  • ??
  • //?Helper?function.?I?included?it?here?so?that?you?can?compile?the?whole?file??
  • //?it?returns?a?random?value?between?min?and?max?included??
  • 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;??
  • ??????
  • ????//?move?the?target?to?a?shaked?position??
  • ????m_pTarget->setPosition(?ccpAdd(ccp(m_initial_x,?m_initial_y),ccp(?randx,?randy)));??
  • }??
  • ??
  • void?CCShake::startWithTarget(CCNode?*pTarget)??
  • {??
  • ????CCActionInterval::startWithTarget(?pTarget?);??
  • ??????
  • ????//?save?the?initial?position??
  • ????m_initial_x?=?pTarget->getPosition().x;??
  • ????m_initial_y?=?pTarget->getPosition().y;??
  • }??
  • ??
  • void?CCShake::stop(void)??
  • {??
  • ????//?Action?is?done,?reset?clip?position??
  • ????this->getTarget()->setPosition(?ccp(?m_initial_x,?m_initial_y?)?);??
  • ??????
  • ????CCActionInterval::stop();??
  • }??
  • ??
  • ///??
  • ??
  • ??
  • CCFallOffShake::CCFallOffShake()??
  • {??
  • ??????
  • }??
  • ??
  • //?Create?the?action?with?a?time?and?a?strength?(same?in?x?and?y)??
  • CCFallOffShake*?CCFallOffShake::create(float?d,?float?strength?)??
  • {??
  • ????return?createWithStrength(?d,?strength,?strength?);??
  • }??
  • ??
  • //?Create?the?action?with?a?time?and?strengths?(different?in?x?and?y)??
  • 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;??
  • ??????
  • ????//?move?the?target?to?a?shaked?position??
  • ????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的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。