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

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

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

#ifndef __FiveSeconds__Shake__
#define __FiveSeconds__Shake__


#include "cocos2d.h"
USING_NS_CC;

class Shake : public ActionInterval
{
public:
    Shake();
    
    // Create the action with a time and a strength (same in x and y)
    static Shake *create(float d,float strength );
    
    // Create the action with a time and strengths (different in x and y)
    static Shake *createWithStrength(float d,float strength_x,float strength_y );
    
    bool initWithDuration(float d,float strength_y );
    
protected:
    
    void startWithTarget(cocos2d::Node *pTarget);
    void update(float time);
    void stop(void);
    
    virtual ActionInterval* reverse() const;
    virtual ActionInterval* clone() const;
    
    Point m_StartPosition;
    Node* m_pTarget;
    
    // Strength of the action
    float m_strength_x,m_strength_y;
};

#endif /* defined(__FiveSeconds__Shake__) */

CPP如下:
#include "Shake.h"
USING_NS_CC;

// not really useful,but I like clean default constructors
Shake::Shake() : m_strength_x(0),m_strength_y(0)
{
}

Shake *Shake::create( float d,float strength )
{
    // call other construction method with twice the same strength
    return createWithStrength( d,strength,strength );
}

Shake *Shake::createWithStrength(float duration,float strength_y)
{
    Shake* pRet = new Shake();
    
    if (pRet && pRet->initWithDuration(duration,strength_x,strength_y))
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }
    
    
    return pRet;
}

bool Shake::initWithDuration(float duration,float strength_y)
{
    if (ActionInterval::initWithDuration(duration))
    {
        m_strength_x = strength_x;
        m_strength_y = strength_y;
        
        return true;
    }
    
    return false;
}

// Helper function. I included it here so that you can compile the whole file
// it returns a random value between min and max included
static float fgRangeRand( float min,float max )
{
    float rnd = ((float)rand() / (float)RAND_MAX);
    return rnd * (max - min) + min;
}

void Shake::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(m_StartPosition+Point(randx,randy));
}

void Shake::startWithTarget(Node *pTarget)
{
    ActionInterval::startWithTarget(pTarget);
    m_pTarget = pTarget;
    // save the initial position
    m_StartPosition=pTarget->getPosition();
}

void Shake::stop(void)
{
    // Action is done,reset clip position
    this->getTarget()->setPosition( m_StartPosition);
    
    ActionInterval::stop();
}

ActionInterval* Shake::reverse() const
{
    return NULL;
}

ActionInterval* Shake::clone() const
{
    return NULL;
}

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...