cocos2d-x 3.0屏幕 抖动特效 重写CCShake

前端之家收集整理的这篇文章主要介绍了cocos2d-x 3.0屏幕 抖动特效 重写CCShake前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这个特效来自http://blog.csdn.net/z104207/article/details/11573271

之前流传的这个代码一直有错误,我使用3.0的方法重新实现了一下

  1. #ifndef __FiveSeconds__Shake__
  2. #define __FiveSeconds__Shake__
  3.  
  4.  
  5. #include "cocos2d.h"
  6. USING_NS_CC;
  7.  
  8. class Shake : public ActionInterval
  9. {
  10. public:
  11. Shake();
  12. // Create the action with a time and a strength (same in x and y)
  13. static Shake *create(float d,float strength );
  14. // Create the action with a time and strengths (different in x and y)
  15. static Shake *createWithStrength(float d,float strength_x,float strength_y );
  16. bool initWithDuration(float d,float strength_y );
  17. protected:
  18. void startWithTarget(cocos2d::Node *pTarget);
  19. void update(float time);
  20. void stop(void);
  21. virtual ActionInterval* reverse() const;
  22. virtual ActionInterval* clone() const;
  23. Point m_StartPosition;
  24. Node* m_pTarget;
  25. // Strength of the action
  26. float m_strength_x,m_strength_y;
  27. };
  28.  
  29. #endif /* defined(__FiveSeconds__Shake__) */

CPP如下:
  1. #include "Shake.h"
  2. USING_NS_CC;
  3.  
  4. // not really useful,but I like clean default constructors
  5. Shake::Shake() : m_strength_x(0),m_strength_y(0)
  6. {
  7. }
  8.  
  9. Shake *Shake::create( float d,float strength )
  10. {
  11. // call other construction method with twice the same strength
  12. return createWithStrength( d,strength,strength );
  13. }
  14.  
  15. Shake *Shake::createWithStrength(float duration,float strength_y)
  16. {
  17. Shake* pRet = new Shake();
  18. if (pRet && pRet->initWithDuration(duration,strength_x,strength_y))
  19. {
  20. pRet->autorelease();
  21. }
  22. else
  23. {
  24. CC_SAFE_DELETE(pRet);
  25. }
  26. return pRet;
  27. }
  28.  
  29. bool Shake::initWithDuration(float duration,float strength_y)
  30. {
  31. if (ActionInterval::initWithDuration(duration))
  32. {
  33. m_strength_x = strength_x;
  34. m_strength_y = strength_y;
  35. return true;
  36. }
  37. return false;
  38. }
  39.  
  40. // Helper function. I included it here so that you can compile the whole file
  41. // it returns a random value between min and max included
  42. static float fgRangeRand( float min,float max )
  43. {
  44. float rnd = ((float)rand() / (float)RAND_MAX);
  45. return rnd * (max - min) + min;
  46. }
  47.  
  48. void Shake::update(float dt)
  49. {
  50. float randx = fgRangeRand( -m_strength_x,m_strength_x ) * dt;
  51. float randy = fgRangeRand( -m_strength_y,m_strength_y ) * dt;
  52. // move the target to a shaked position
  53. m_pTarget->setPosition(m_StartPosition+Point(randx,randy));
  54. }
  55.  
  56. void Shake::startWithTarget(Node *pTarget)
  57. {
  58. ActionInterval::startWithTarget(pTarget);
  59. m_pTarget = pTarget;
  60. // save the initial position
  61. m_StartPosition=pTarget->getPosition();
  62. }
  63.  
  64. void Shake::stop(void)
  65. {
  66. // Action is done,reset clip position
  67. this->getTarget()->setPosition( m_StartPosition);
  68. ActionInterval::stop();
  69. }
  70.  
  71. ActionInterval* Shake::reverse() const
  72. {
  73. return NULL;
  74. }
  75.  
  76. ActionInterval* Shake::clone() const
  77. {
  78. return NULL;
  79. }

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