COCOS2D-X 抖动效果 CCShake
来源:互联网 发布:湖南卫视网络在线直播 编辑:程序博客网 时间:2024/05/16 12:48
cocos2dx全屏抖动,个别对象抖动
- /**
- 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]
- */
- class CCShake : public cocos2d::CCActionInterval
- {
- public:
- CCShake();
- // Create the action with a time and a strength (same in x and y)
- static CCShake* create(float d, float strength );
- // Create the action with a time and strengths (different in x and y)
- static CCShake* createWithStrength(float d, float strength_x, float strength_y );
- bool initWithDuration(float d, float strength_x, float strength_y );
- protected:
- virtual void startWithTarget(cocos2d::CCNode *pTarget);
- virtual void update(float time);
- virtual void stop(void);
- // Initial position of the shaked node
- float m_initial_x, m_initial_y;
- // Strength of the action
- float m_strength_x, m_strength_y;
- };
- /**
- * 线性抖动(剩余时间越短,抖动范围越小)
- */
- class CCFallOffShake : public CCShake
- {
- public:
- CCFallOffShake();
- // Create the action with a time and a strength (same in x and y)
- static CCFallOffShake* create(float d, float strength );
- // Create the action with a time and strengths (different in x and y)
- static CCFallOffShake* createWithStrength(float d, float strength_x, float strength_y );
- protected:
- virtual void update(float time);
- };
- #endif //__SHAKE_H__
- #include "ShakeAction.h"
- #include "cocos2d.h"
- USING_NS_CC;
- // not really useful, but I like clean default constructors
- CCShake::CCShake() : m_strength_x(0), m_strength_y(0), m_initial_x(0), m_initial_y(0)
- {
- }
- CCShake* CCShake::create( float d, float strength )
- {
- // call other construction method with twice the same strength
- return createWithStrength( d, strength, strength );
- }
- CCShake* CCShake::createWithStrength(float duration, float strength_x, float strength_y)
- {
- CCShake *pRet = new CCShake();
- if (pRet && pRet->initWithDuration(duration, strength_x, strength_y))
- {
- pRet->autorelease();
- }
- else
- {
- CC_SAFE_DELETE(pRet);
- }
- return pRet;
- }
- bool CCShake::initWithDuration(float duration, float strength_x, float strength_y)
- {
- if (CCActionInterval::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 = CCRANDOM_0_1();
- return rnd*(max-min)+min;
- }
- void CCShake::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( ccpAdd(ccp(m_initial_x, m_initial_y),ccp( randx, randy)));
- }
- void CCShake::startWithTarget(CCNode *pTarget)
- {
- CCActionInterval::startWithTarget( pTarget );
- // save the initial position
- m_initial_x = pTarget->getPosition().x;
- m_initial_y = pTarget->getPosition().y;
- }
- void CCShake::stop(void)
- {
- // Action is done, reset clip position
- this->getTarget()->setPosition( ccp( m_initial_x, m_initial_y ) );
- CCActionInterval::stop();
- }
- ///////////////////////////////////////////////////////////////////////////////
- CCFallOffShake::CCFallOffShake()
- {
- }
- // Create the action with a time and a strength (same in x and y)
- CCFallOffShake* CCFallOffShake::create(float d, float strength )
- {
- return createWithStrength( d, strength, strength );
- }
- // Create the action with a time and strengths (different in x and y)
- CCFallOffShake* CCFallOffShake::createWithStrength(float duration, float strength_x, float strength_y)
- {
- CCFallOffShake *pRet = new CCFallOffShake();
- if (pRet && pRet->initWithDuration(duration, strength_x, strength_y))
- {
- pRet->autorelease();
- }
- else
- {
- CC_SAFE_DELETE(pRet);
- }
- return pRet;
- }
- void CCFallOffShake::update(float dt)
- {
- float rate = (getDuration() - getElapsed())/getDuration();
- if (rate < 0.f) {
- rate = 0.f;
- }
- float randx = fgRangeRand( -m_strength_x, m_strength_x )*rate;
- float randy = fgRangeRand( -m_strength_y, m_strength_y )*rate;
- // move the target to a shaked position
- m_pTarget->setPosition( ccpAdd(ccp(m_initial_x, m_initial_y),ccp( randx, randy)));
- }
用法:
pSprite->runAction(CCShake::create(0.1f,10));
pSprite:想抖动的物体
第一个参数是:抖动的时间
第一个参数是:抖动的幅度
注意
一个CCNode同时执行多个CCShake动作,或者一个CCShake没有完又执行一个CCShake的话就会出现问题,会出现偏移的现象!
解决方案:
1).不要同时执行多个CCShake动作.
2.自己外部记录这个CCNode的位置,执行完成后手动setPosition();
0 0
- COCOS2D-X 抖动效果 CCShake
- COCOS2D-X 抖动效果 CCShake
- COCOS2D-X 抖动效果 CCShake
- cocos2d-x 使整个游戏场景抖动CCShake
- cocos2d-x 3.0屏幕 抖动特效 重写CCShake
- Cocos2d-x 抖动效果
- cocos2d-x (3.0)抖动效果
- 【COCOS2DX-游戏开发之三十】抖动效果 CCShake
- 【COCOS2DX-游戏开发之三十】抖动效果 CCShake
- cocos2d-x-3.3rc0退出抖动问题
- cocos2d-x橡皮擦效果---
- cocos2d-x 手电筒效果
- Cocos2d-x粒子效果
- cocos2d-x 手电筒效果
- cocos2d-x 效果
- cocos2d-x 粒子效果
- cocos2d-x橡皮擦效果---
- cocos2d-x button 效果
- LFM推荐的例子和代码
- 6. ZigZag Conversion【E】【10】【leetcode】
- POJ 1679The Unique MST 次小生成树
- LeetCode no. 35
- PHP之创建图像验证码
- COCOS2D-X 抖动效果 CCShake
- 直方图均衡化 Histogram Equalization
- (一)mybatis学习之初识mybatis
- softmax的log似然代价函数(公式求导)
- python爬虫学习--pixiv爬虫(2)--国际排行榜的图片爬取
- 第六周项目1-分数类的雏形
- css入门
- PHP之创建图像验证码 ---实例
- Android动画原理分析