cocos2dx全屏抖动,个别对象抖动
-
-
- 一个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
#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(//Createtheactionwithatimeandstrengths(differentinxandy)
- 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(); (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|