CCShake. h [cpp] # ifndef _ SHAKE_H _ # define _ SHAKE_H _ # include "CCActionInterval. h "class CCShake: public cocos2d: CCActionInterval {// Code by Francois Guibert // Contact: www.frozax.com- http://twitter.com/frozax -Www.facebook.com/frozax public: CCShake (); // Create the action with a time and a strength (same in x and y) static CCShake * create (float d, float strength ); // Create the action with a time and strengths (different in x and y) static CCShake * createWithStrength (float d, float strength_x, float strength_y); bool initWithDuration (float d, float strength_x, float strength_y); protected: void startWithTarget (cocos2d: CCNode * pTarget); void update (float time); void stop (void ); // Initial position of the shaked node float m_initial_x, m_initial_y; // Strength of the action float m_strength_x, m_strength_y ;}; # endif/_ SHAKE_H _ # ifndef _ SHAKE_H __# define _ SHAKE_H _ # include "CCActionInterval. h "class CCShake: public cocos2d: CCActionInterval {// Code by Francois Guibert // Contact: www.frozax.com- http://twitter.com/frozax -Www.facebook.com/frozaxpublic: CCShake (); // Create the action with a time and a strength (same in x and y) static CCShake * create (float d, float strength ); // Create the action with a time and strengths (different in x and y) static CCShake * createWithStrength (float d, float strength_x, float strength_y); bool initWithDuration (float d, float strength_x, float strength_y); protected: void startWithTarget (cocos2d: CCNode * pTarget); void update (float time); void stop (void ); // Initial position of the shaked node float m_initial_x, m_initial_y; // Strength of the action float m_strength_x, m_strength_y;}; # endif/_ SHAKE_H _ CCShake. cpp [cpp] // Code by Francois Guibert // Contact: www.frozax.com- http://twitter.com/frozax -Www.facebook.com/frozax # include "CCShake. h "# include" cocos2d. h "USING_NS_CC; // not really useful, but I like clean default constructors CCShake: CCShake (): m_strength_x (0), m_strength_y (0), m_initial_x (0 ), m_initial_y (0) {} CCShake * CCShake: create (float d, float strength) {// call other construction method with twice the same strength return createWithStrength (d, strength, strength);} CCShake * CCShake: createWithStrength (float duration, float strength_x, float strength_y) {CCShake * pRet = new CCShake (); if (pRet & pRet-> initWithDuration (duration, strength_x, strength_y) {pRet-> autorelease ();} else {CC_SAFE_DELETE (pRet);} return pRet ;} bool CCShake: equals (float duration, float strength_x, float strength_y) {if (CCActionInterval: duration) {m_strength_x = strength_x; Limit = strength_y; return true ;} return false;} // Helper function. I have ded it here so that you can compile the whole file // it returns a random value between min and max has ded static float fgRangeRand (float min, float max) {float rnd = (float) rand ()/(float) RAND_MAX); return rnd * (max-min) + min;} void CCShake: update (float dt) {float randx = fgRangeRand (-m_strength_x, m_strength_x) * dt; float randy = fgRangeRand (-m_strength_y, m_strength_y) * dt; // move the target to a shaked position m_pTarget-> setPosition (ccpAdd (ccp (m_initial_x, m_initial_y), ccp (randx, randy);} void CCShake :: startWithTarget (CCNode * pTarget) {CCActionInterval: startWithTarget (pTarget); // save the initial position m_initial_x = pTarget-> getPosition (). x; m_initial_y = pTarget-> getPosition (). y;} void CCShake: stop (void) {// Action is done, reset clip position this-> getTarget ()-> setPosition (ccp (m_initial_x, m_initial_y )); CCActionInterval: stop ();} // Code by Francois Guibert // Contact: www.frozax.com- http://twitter.com/frozax -Www.facebook.com/frozax # include "CCShake. h "# include" cocos2d. h "USING_NS_CC; // not really useful, but I like clean default constructorsCCShake: CCShake (): m_strength_x (0), m_strength_y (0), m_initial_x (0 ), m_initial_y (0) {} CCShake * CCShake: create (float d, float strength) {// call other construction method with twice the same strength return createWithStrength (d, strength, strength);} CCShake * CCShake: createWithStrength (float duration, float strength_x, float strength_y) {CCShake * pRet = new CCShake (); if (pRet & pRet-> initWithDuration (duration, strength_x, strength_y) {pRet-> autorelease ();} else {CC_SAFE_DELETE (pRet);} return pRet ;} bool CCShake: equals (float duration, float strength_x, float strength_y) {if (CCActionInterval: duration) {m_strength_x = strength_x; Limit = strength_y; return true ;} return false;} // Helper function. I have ded it here so that you can compile the whole file // it returns a random value between min and max values dedstatic float fgRangeRand (float min, float max) {float rnd = (float) rand ()/(float) RAND_MAX); return rnd * (max-min) + min;} void CCShake: update (float dt) {float randx = fgRangeRand (-m_strength_x, m_strength_x) * dt; float randy = fgRangeRand (-m_strength_y, m_strength_y) * dt; // move the target to a shaked position m_pTarget-> setPosition (ccpAdd (ccp (m_initial_x, m_initial_y), ccp (randx, randy);} void CCShake :: startWithTarget (CCNode * pTarget) {www.2cto.com CCActionInterval: startWithTarget (pTarget); // save the initial position m_initial_x = pTarget-> getPosition (). x; m_initial_y = pTarget-> getPosition (). y;} void CCShake: stop (void) {// Action is done, reset clip position this-> getTarget ()-> setPosition (ccp (m_initial_x, m_initial_y )); CCActionInterval: stop ();}