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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- c# – Umlaute编码错误!
- ruby-on-rails – 更好的替代尝试(:输出).try(:data).try
- Oracle expdp / impdp 用法详解
- quick-cocos2d-x 中的 socket 技术选择:LuaSocket 和 WebS
- 分享ORACLE SEQUENCE跳号总结
- c# – User.Identity.GetUserId是否已缓存或每次从数据库中
- cocos2dx3.2开发 RPG《Flighting》(二)骨骼动画介绍及游戏
- 正则表达式:ios 手机号车牌号 正则验证
- objective-c – 在Interface Builder中将自定义视图拖到窗口
- SQLite3 C/C++编程接口介绍