COCOS2D-X 抖动效果 CCShake

游戏中屏幕抖动是一个非常炫的效果,那么在COCOS2D-X  中该怎么实现呢?


直接上代码:

CCShake.h


[cpp]  view plain copy
  1. #ifndef __SHAKE_H__  
  2. #define __SHAKE_H__  
  3.   
  4. #include "cocos2d.h"  
  5. USING_NS_CC;  
  6.   
  7.   
  8. class CCShake : public cocos2d::CCActionInterval  
  9. {  
  10. public:  
  11.     CCShake();  
  12.   
  13.     // Create the action with a time and a strength (same in x and y)  
  14.     static CCShake *create(float d, float strength );  
  15.     // Create the action with a time and strengths (different in x and y)  
  16.     static CCShake *createWithStrength(float d, float strength_x, float strength_y );  
  17.     bool initWithDuration(float d, float strength_x, float strength_y );  
  18.   
  19. protected:  
  20.   
  21.     void startWithTarget(cocos2d::CCNode *pTarget);  
  22.     void update(float time);  
  23.     void stop(void);  
  24.   
  25.     CCPoint m_StartPosition;  
  26.   
  27.   
  28.     // Strength of the action  
  29.     float m_strength_x, m_strength_y;  
  30. };  
  31.   
  32. #endif //__SHAKE_H__  

------------------------------------------------------------------------------------------------------------------------------------

CCShake.cpp

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

用法:


pSprite->runAction(CCShake::create(0.1f,10));
 pSprite:想抖动的物体


第一个参数是:抖动的时间

第一个参数是:抖动的幅度


注意

一个CCNode同时执行多个CCShake动作,或者一个CCShake没有完又执行一个CCShake的话就会出现问题,会出现偏移的现象!

解决方案:

1).不要同时执行多个CCShake动作.

2.自己外部记录这个CCNode的位置,执行完成后手动setPosition();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值