COCOS2D-X 抖动效果 CCShake

前端之家收集整理的这篇文章主要介绍了COCOS2D-X 抖动效果 CCShake前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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


  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__
  37. @H_502_287@



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. }
  54. @H_502_287@


用法:

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

原文链接:https://www.f2er.com/cocos2dx/339924.html

猜你在找的Cocos2d-x相关文章