加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

COCOS2D-X 抖动效果 CCShake

发布时间:2020-12-14 16:59:42 所属栏目:百科 来源:网络整理
导读:cocos2dx全屏抖动,个别对象抖动 [cpp] view plain copy /** desc:让指定控件抖动 一个CCNode同时执行多个CCShake动作,或者一个CCShake没有完又执行一个CCShake的话就会出现问题,会出现偏移的现象! 解决方案: 1).不要同时执行多个CCShake动作. 2.自己外部记

cocos2dx全屏抖动,个别对象抖动


[cpp] view plain copy
  1. /**
  2. desc:让指定控件抖动
  3. 一个CCNode同时执行多个CCShake动作,或者一个CCShake没有完又执行一个CCShake的话就会出现问题,会出现偏移的现象!
  4. 解决方案:
  5. 1).不要同时执行多个CCShake动作.
  6. 2.自己外部记录这个CCNode的位置,执行完成后手动setPosition();
  7. */
  8. #ifndef__SHAKE_H__
  9. #define__SHAKE_H__
  10. #include"actions/CCActionInterval.h"
  11. /**
  12. *按指定频度范围内抖动[-strength_x,strength_x][-strength_y,strength_y]
  13. classCCShake:publiccocos2d::CCActionInterval
  14. {
  15. public:
  16. CCShake();
  17. //Createtheactionwithatimeandastrength(sameinxandy)
  18. staticCCShake*create(floatd,floatstrength);
  19. //Createtheactionwithatimeandstrengths(differentinxandy)
  20. staticCCShake*createWithStrength(floatstrength_x,87); font-weight:bold; background-color:inherit">floatstrength_y);
  21. boolinitWithDuration(floatstrength_y);
  22. protected:
  23. virtualvoidstartWithTarget(cocos2d::CCNode*pTarget);
  24. voidupdate(floattime);
  25. voidstop(void);
  26. //Initialpositionoftheshakednode
  27. floatm_initial_x,m_initial_y;
  28. //Strengthoftheaction
  29. floatm_strength_x,m_strength_y;
  30. };
  31. *线性抖动(剩余时间越短,抖动范围越小)
  32. classCCFallOffShake:publicCCShake
  33. CCFallOffShake();
  34. staticCCFallOffShake*create(staticCCFallOffShake*createWithStrength(protected:
  35. floattime);
  36. #endif//__SHAKE_H__



CPP


copy
    #include"ShakeAction.h"
  1. #include"cocos2d.h"
  2. USING_NS_CC;
  3. //notreallyuseful,butIlikecleandefaultconstructors
  4. CCShake::CCShake():m_strength_x(0),m_strength_y(0),m_initial_x(0),m_initial_y(0)
  5. {
  6. }
  7. CCShake*CCShake::create(floatstrength)
  8. //callotherconstructionmethodwithtwicethesamestrength
  9. returncreateWithStrength(d,strength,strength);
  10. CCShake*CCShake::createWithStrength(floatduration,87); font-weight:bold; background-color:inherit">floatstrength_y)
  11. CCShake*pRet=newCCShake();
  12. if(pRet&&pRet->initWithDuration(duration,strength_x,strength_y))
  13. pRet->autorelease();
  14. }
  15. else
  16. CC_SAFE_DELETE(pRet);
  17. returnpRet;
  18. boolCCShake::initWithDuration(floatstrength_y)
  19. if(CCActionInterval::initWithDuration(duration))
  20. m_strength_x=strength_x;
  21. m_strength_y=strength_y;
  22. returntrue;
  23. false;
  24. //Helperfunction.Iincludeditheresothatyoucancompilethewholefile
  25. //itreturnsarandomvaluebetweenminandmaxincluded
  26. staticfloatfgRangeRand(floatmin,87); font-weight:bold; background-color:inherit">floatmax)
  27. floatrnd=CCRANDOM_0_1();
  28. returnrnd*(max-min)+min;
  29. voidCCShake::update(floatdt)
  30. floatrandx=fgRangeRand(-m_strength_x,m_strength_x)*dt;
  31. floatrandy=fgRangeRand(-m_strength_y,m_strength_y)*dt;
  32. //movethetargettoashakedposition
  33. m_pTarget->setPosition(ccpAdd(ccp(m_initial_x,m_initial_y),ccp(randx,randy)));
  34. voidCCShake::startWithTarget(CCNode*pTarget)
  35. CCActionInterval::startWithTarget(pTarget);
  36. //savetheinitialposition
  37. m_initial_x=pTarget->getPosition().x;
  38. m_initial_y=pTarget->getPosition().y;
  39. voidCCShake::stop(void)
  40. //Actionisdone,resetclipposition
  41. this->getTarget()->setPosition(ccp(m_initial_x,m_initial_y));
  42. CCActionInterval::stop();
  43. ///////////////////////////////////////////////////////////////////////////////
  44. CCFallOffShake::CCFallOffShake()
  45. //Createtheactionwithatimeandastrength(sameinxandy)
  46. CCFallOffShake*CCFallOffShake::create(//Createtheactionwithatimeandstrengths(differentinxandy)
  47. CCFallOffShake*CCFallOffShake::createWithStrength( CCFallOffShake*pRet=newCCFallOffShake();
  48. returnpRet;
  49. voidCCFallOffShake::update(floatdt)
  50. floatrate=(getDuration()-getElapsed())/getDuration();
  51. if(rate<0.f){
  52. rate=0.f;
  53. }


用法:

pSprite->runAction(CCShake::create(0.1f,10));
pSprite:想抖动的物体
第一个参数是:抖动的时间
第一个参数是:抖动的幅度
注意
一个CCNode同时执行多个CCShake动作,会出现偏移的现象!
解决方案:
1).不要同时执行多个CCShake动作.
2.自己外部记录这个CCNode的位置,执行完成后手动setPosition();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读