转自:http://blog.sina.com.cn/s/blog_7cbd10170102uxi1.HTML
最近使用cocos2dx需要用到一个需求,就是关于图片精灵抖动的动作效果。稍微查了一下,找到一个CCShake用来实现这样效果的。不过网上几乎都是在2.x平台上的。所以我根据需求把它改成能用在3.x平台上的。下面放一下代码先:
//// CCShake.h//// Code by Francois Guibert// Contact: www.frozax.com - http://twitter.com/frozax - www.facebook.com/frozax//// 何遵祖修改于2014.7.10 支持cocos2dx 3.0 修正了动作执行精灵位置错误问题 测试环境:cocos2d-x-3.0rc1#ifndef __war__CCShake__#define __war__CCShake__#include#include "cocos2d.h"using namespace cocos2d;class Shake : public ActionInterval{public: Shake(); // Create the action with a time and a strength (same in x and y) // 产生震动效果的初始化函数参数,两个方向相同 // @param d 震动持续的时间 // @param strength 震动的幅度 static Shake* create(float d,float strength); // Create the action with a time and strengths (different in x and y) // 产生震动效果的初始化函数,两个方向值不一样 static Shake* create(float d,float strength_x,float strength_y); bool initWithDuration(float d,float strength_y); //以下都是重写父类抽象类的函数(必须重写) virtual Shake* clone() const overrIDe; virtual Shake* reverse(voID) const overrIDe; virtual voID startWithTarget(Node *target) overrIDe; virtual voID update(float time) overrIDe; virtual voID stop(voID); protected: // Initial position of the shaked node // 精灵的位置 float _initial_x,_initial_y; // Strength of the action // 抖动的幅度 float _strength_x,_strength_y;};
//// Shake.cpp// war//#include "CCShake.h"// not really useful,but I like clean default constructorsShake::Shake() : _strength_x(0),_strength_y(0),_initial_x(0),_initial_y(0){}Shake* Shake::create(float d,float strength ){ // call other construction method with twice the same strength return create( d,strength,strength );}Shake* Shake::create(float duration,float strength_y){ Shake *p_action = new Shake(); p_action->initWithDuration(duration,strength_x,strength_y); p_action->autorelease(); return p_action;}bool Shake::initWithDuration(float duration,float strength_y){ if (CCActionInterval::initWithDuration(duration)) { _strength_x = strength_x; _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 includedfloat fgRangeRand( float min,float max ){ float rnd = ((float)rand()/(float)RAND_MAX); return rnd*(max-min)+min;}voID Shake::update(float time){ float randx = fgRangeRand( -_strength_x,_strength_x ); float randy = fgRangeRand( -_strength_y,_strength_y ); // move the target to a shaked position _target->setposition(Vec2(_initial_x + randx,_initial_y + randy));}Shake* Shake::clone(voID) const{ auto a = new Shake(); a->initWithDuration(_duration,_strength_x,_strength_y); a->autorelease(); return a;}Shake* Shake::reverse() const{ return Shake::create(_duration,-_strength_x,-_strength_y);}voID Shake::startWithTarget(Node *target){ CCActionInterval::startWithTarget(target); // save the initial position _initial_x = target->getposition().x; _initial_y = target->getposition().y;}voID Shake::stop(voID){ // Action is done,reset clip position _target->setposition(Vec2( _initial_x,_initial_y ) ); CCActionInterval::stop();}
在使用过程中,由于某处设置错误,导致shake动作被调用两次,完成动作后精灵偏离了原来的位置。所以,切记,不能被同时执行两个shake动作。
总结以上是内存溢出为你收集整理的cocos2d-x (3.0)抖动效果全部内容,希望文章能够帮你解决cocos2d-x (3.0)抖动效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)