COCOS2D-X 抖动效果 CCShake

COCOS2D-X 抖动效果 CCShake,第1张

概述cocos2dx全屏抖动,个别对象抖动 [cpp]  view plain  copy /**   desc:让指定控件抖动   一个CCNode同时执行多个CCShake动作,或者一个CCShake没有完又执行一个CCShake的话就会出现问题,会出现偏移的现象!      解决方案:   1).不要同时执行多个CCShake动作.   2.自己外部记录这个CCNode的位置,执行完成后手动se

cocos2dx全屏抖动,个别对象抖动


[cpp] view plain copy /** desc:让指定控件抖动 一个CCNode同时执行多个CCShake动作,或者一个CCShake没有完又执行一个CCShake的话就会出现问题,会出现偏移的现象! 解决方案: 1).不要同时执行多个CCShake动作. 2.自己外部记录这个CCNode的位置,执行完成后手动setposition(); */ #ifndef__SHAKE_H__ #define__SHAKE_H__ #include"actions/CCActionInterval.h" /** *按指定频度范围内抖动[-strength_x,strength_x][-strength_y,strength_y] classCCShake:publiccocos2d::CCActionInterval { public: CCShake(); //Createtheactionwithatimeandastrength(sameinxandy) staticCCShake*create(floatd,floatstrength); //Createtheactionwithatimeandstrengths(differentinxandy) staticCCShake*createWithStrength(floatstrength_x,87); Font-weight:bold; background-color:inherit">floatstrength_y); boolinitWithDuration(floatstrength_y); protected: virtualvoIDstartWithTarget(cocos2d::CCNode*pTarget); voIDupdate(floattime); voIDstop(voID); //Initialpositionoftheshakednode floatm_initial_x,m_initial_y; //Strengthoftheaction floatm_strength_x,m_strength_y; }; *线性抖动(剩余时间越短,抖动范围越小) classCCFallOffShake:publicCCShake CCFallOffShake(); staticCCFallOffShake*create(staticCCFallOffShake*createWithStrength(protected: floattime); #endif//__SHAKE_H__


CPP


copy #include"ShakeAction.h" #include"cocos2d.h" USING_NS_CC; //notreallyuseful,butIlikecleandefaultconstructors CCShake::CCShake():m_strength_x(0),m_strength_y(0),m_initial_x(0),m_initial_y(0) { } CCShake*CCShake::create(floatstrength) //callotherconstructionmethoDWithtwicethesamestrength returncreateWithStrength(d,strength,strength); CCShake*CCShake::createWithStrength(floatduration,87); Font-weight:bold; background-color:inherit">floatstrength_y) CCShake*pRet=newCCShake(); if(pRet&&pRet->initWithDuration(duration,strength_x,strength_y)) pRet->autorelease(); } else CC_SAFE_DELETE(pRet); returnpRet; boolCCShake::initWithDuration(floatstrength_y) if(CCActionInterval::initWithDuration(duration)) m_strength_x=strength_x; m_strength_y=strength_y; returntrue; false; //Helperfunction.Iincludeditheresothatyoucancompilethewholefile //itreturnSarandomvaluebetweenminandmaxincluded staticfloatfgRangeRand(floatmin,87); Font-weight:bold; background-color:inherit">floatmax) floatrnd=CCRANDOM_0_1(); returnrnd*(max-min)+min; voIDCCShake::update(floatdt) floatrandx=fgRangeRand(-m_strength_x,m_strength_x)*dt; floatrandy=fgRangeRand(-m_strength_y,m_strength_y)*dt; //movethetargettoashakedposition m_pTarget->setposition(ccpAdd(ccp(m_initial_x,m_initial_y),ccp(randx,randy))); voIDCCShake::startWithTarget(CCNode*pTarget) CCActionInterval::startWithTarget(pTarget); //savetheinitialposition m_initial_x=pTarget->getposition().x; m_initial_y=pTarget->getposition().y; voIDCCShake::stop(voID) //Actionisdone,resetclipposition this->getTarget()->setposition(ccp(m_initial_x,m_initial_y)); CCActionInterval::stop(); /////////////////////////////////////////////////////////////////////////////// CCFallOffShake::CCFallOffShake() //Createtheactionwithatimeandastrength(sameinxandy) CCFallOffShake*CCFallOffShake::create( CCFallOffShake*CCFallOffShake::createWithStrength( CCFallOffShake*pRet=newCCFallOffShake(); returnpRet; voIDCCFallOffShake::update(floatdt) floatrate=(getDuration()-getElapsed())/getDuration(); if(rate<0.f){ rate=0.f; }

用法:

pSprite->runAction(CCShake::create(0.1f,10));
pSprite:想抖动的物体
第一个参数是:抖动的时间
第一个参数是:抖动的幅度
注意
一个CCNode同时执行多个CCShake动作,会出现偏移的现象!
解决方案:
1).不要同时执行多个CCShake动作.
2.自己外部记录这个CCNode的位置,执行完成后手动setposition();

总结

以上是内存溢出为你收集整理的COCOS2D-X 抖动效果 CCShake全部内容,希望文章能够帮你解决COCOS2D-X 抖动效果 CCShake所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1078848.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存