加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 19:23:45 所属栏目:百科 来源:网络整理
导读:这个特效来自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

这个特效来自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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读