• COCOS2D-X 抖动效果 CCShake


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


    [cpp] view plain copy
    1. /** 
    2.  desc:让指定控件抖动 
    3.  一个CCNode同时执行多个CCShake动作,或者一个CCShake没有完又执行一个CCShake的话就会出现问题,会出现偏移的现象! 
    4.   
    5.  解决方案: 
    6.  1).不要同时执行多个CCShake动作. 
    7.  2.自己外部记录这个CCNode的位置,执行完成后手动setPosition(); 
    8.  */  
    9. #ifndef __SHAKE_H__  
    10. #define __SHAKE_H__  
    11.   
    12. #include "actions/CCActionInterval.h"  
    13.   
    14. /** 
    15.  * 按指定频度范围内抖动[-strength_x,strength_x][-strength_y, strength_y] 
    16.  */  
    17. class CCShake : public cocos2d::CCActionInterval  
    18. {  
    19. public:  
    20.     CCShake();  
    21.       
    22.     // Create the action with a time and a strength (same in x and y)  
    23.     static CCShake* create(float d, float strength );  
    24.     // Create the action with a time and strengths (different in x and y)  
    25.     static CCShake* createWithStrength(float d, float strength_x, float strength_y );  
    26.     bool initWithDuration(float d, float strength_x, float strength_y );  
    27.       
    28. protected:  
    29.       
    30.     virtual void startWithTarget(cocos2d::CCNode *pTarget);  
    31.     virtual void update(float time);  
    32.     virtual void stop(void);  
    33.       
    34.       
    35.     // Initial position of the shaked node  
    36.     float m_initial_x, m_initial_y;  
    37.     // Strength of the action  
    38.     float m_strength_x, m_strength_y;  
    39. };  
    40.   
    41.   
    42. /** 
    43.  * 线性抖动(剩余时间越短,抖动范围越小) 
    44.  */  
    45. class CCFallOffShake : public CCShake  
    46. {  
    47. public:  
    48.     CCFallOffShake();  
    49.       
    50.     // Create the action with a time and a strength (same in x and y)  
    51.     static CCFallOffShake* create(float d, float strength );  
    52.     // Create the action with a time and strengths (different in x and y)  
    53.     static CCFallOffShake* createWithStrength(float d, float strength_x, float strength_y );  
    54.       
    55. protected:  
    56.     virtual void update(float time);  
    57. };  
    58.   
    59. #endif //__SHAKE_H__  



    CPP


    [cpp] view plain copy
    1. #include "ShakeAction.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), m_initial_x(0), m_initial_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.         return true;  
    40.     }  
    41.       
    42.     return false;  
    43. }  
    44.   
    45. // Helper function. I included it here so that you can compile the whole file  
    46. // it returns a random value between min and max included  
    47. static float fgRangeRand( float min, float max )  
    48. {  
    49.     float rnd = CCRANDOM_0_1();  
    50.     return rnd*(max-min)+min;  
    51. }  
    52.   
    53. void CCShake::update(float dt)  
    54. {  
    55.     float randx = fgRangeRand( -m_strength_x, m_strength_x )*dt;  
    56.     float randy = fgRangeRand( -m_strength_y, m_strength_y )*dt;  
    57.       
    58.     // move the target to a shaked position  
    59.     m_pTarget->setPosition( ccpAdd(ccp(m_initial_x, m_initial_y),ccp( randx, randy)));  
    60. }  
    61.   
    62. void CCShake::startWithTarget(CCNode *pTarget)  
    63. {  
    64.     CCActionInterval::startWithTarget( pTarget );  
    65.       
    66.     // save the initial position  
    67.     m_initial_x = pTarget->getPosition().x;  
    68.     m_initial_y = pTarget->getPosition().y;  
    69. }  
    70.   
    71. void CCShake::stop(void)  
    72. {  
    73.     // Action is done, reset clip position  
    74.     this->getTarget()->setPosition( ccp( m_initial_x, m_initial_y ) );  
    75.       
    76.     CCActionInterval::stop();  
    77. }  
    78.   
    79. ///////////////////////////////////////////////////////////////////////////////  
    80.   
    81.   
    82. CCFallOffShake::CCFallOffShake()  
    83. {  
    84.       
    85. }  
    86.   
    87. // Create the action with a time and a strength (same in x and y)  
    88. CCFallOffShake* CCFallOffShake::create(float d, float strength )  
    89. {  
    90.     return createWithStrength( d, strength, strength );  
    91. }  
    92.   
    93. // Create the action with a time and strengths (different in x and y)  
    94. CCFallOffShake* CCFallOffShake::createWithStrength(float duration, float strength_x, float strength_y)  
    95. {  
    96.     CCFallOffShake *pRet = new CCFallOffShake();  
    97.       
    98.     if (pRet && pRet->initWithDuration(duration, strength_x, strength_y))  
    99.     {  
    100.         pRet->autorelease();  
    101.     }  
    102.     else  
    103.     {  
    104.         CC_SAFE_DELETE(pRet);  
    105.     }  
    106.       
    107.     return pRet;  
    108. }  
    109.   
    110. void CCFallOffShake::update(float dt)  
    111. {  
    112.     float rate = (getDuration() - getElapsed())/getDuration();  
    113.     if (rate < 0.f) {  
    114.         rate = 0.f;  
    115.     }  
    116.       
    117.     float randx = fgRangeRand( -m_strength_x, m_strength_x )*rate;  
    118.     float randy = fgRangeRand( -m_strength_y, m_strength_y )*rate;  
    119.       
    120.     // move the target to a shaked position  
    121.     m_pTarget->setPosition( ccpAdd(ccp(m_initial_x, m_initial_y),ccp( randx, randy)));  
    122. }  


    用法:


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


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

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


    注意

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

    解决方案:

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

    2.自己外部记录这个CCNode的位置,执行完成后手动setPosition();


  • 相关阅读:
    14款响应式布局的前端开发框架
    如何在本地进行微信公众号的开发调试
    微信公众帐号应用开发—本地调试
    利用H5开发微信公众号
    微信公众平台开发入门教程
    JAVA编程不得不看的几本经典书籍
    使用百度UMeditor富文本编辑器,修改自定义图片上传,修改源码
    网站建设需要哪些程序
    Visual Studio 2013 中使用断点
    Objective-c 实例变量的访问级别
  • 原文地址:https://www.cnblogs.com/Anzhongliu/p/6091822.html
Copyright © 2020-2023  润新知