COCOS2D-X 抖动效果 CCShake
2013-09-11 17:12
239 查看
游戏中屏幕抖动是一个非常炫的效果,那么在COCOS2D-X 中该怎么实现呢?
直接上代码:
CCShake.h
------------------------------------------------------------------------------------------------------------------------------------
CCShake.cpp
用法:
pSprite->runAction(CCShake::create(0.1f,10));
pSprite:想抖动的物体
第一个参数是:抖动的时间
第一个参数是:抖动的幅度
注意
一个CCNode同时执行多个CCShake动作,或者一个CCShake没有完又执行一个CCShake的话就会出现问题,会出现偏移的现象!
解决方案:
1).不要同时执行多个CCShake动作.
2.自己外部记录这个CCNode的位置,执行完成后手动setPosition();
直接上代码:
CCShake.h
#ifndef __SHAKE_H__ #define __SHAKE_H__ #include "cocos2d.h" USING_NS_CC; 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: void startWithTarget(cocos2d::CCNode *pTarget); void update(float time); void stop(void); CCPoint m_StartPosition; // Strength of the action float m_strength_x, m_strength_y; }; #endif //__SHAKE_H__
------------------------------------------------------------------------------------------------------------------------------------
CCShake.cpp
#include "CCShake.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) { } 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 = ((float)rand() / (float)RAND_MAX); 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(m_StartPosition, ccp( randx, randy))); } void CCShake::startWithTarget(CCNode *pTarget) { CCActionInterval::startWithTarget( pTarget ); // save the initial position m_StartPosition=pTarget->getPosition(); } void CCShake::stop(void) { // Action is done, reset clip position this->getTarget()->setPosition( m_StartPosition); CCActionInterval::stop(); }
用法:
pSprite->runAction(CCShake::create(0.1f,10));
pSprite:想抖动的物体
第一个参数是:抖动的时间
第一个参数是:抖动的幅度
注意
一个CCNode同时执行多个CCShake动作,或者一个CCShake没有完又执行一个CCShake的话就会出现问题,会出现偏移的现象!
解决方案:
1).不要同时执行多个CCShake动作.
2.自己外部记录这个CCNode的位置,执行完成后手动setPosition();
相关文章推荐
- COCOS2D-X 抖动效果 CCShake
- COCOS2D-X 抖动效果 CCShake
- cocos2d-x 使整个游戏场景抖动CCShake
- 【COCOS2DX-游戏开发之三十】抖动效果 CCShake
- cocos2d-x (3.0)抖动效果
- Cocos2d-x 抖动效果
- 【COCOS2DX-游戏开发之三十】抖动效果 CCShake
- cocos2d-x 3.0屏幕 抖动特效 重写CCShake
- cocos2d-x button 效果
- # 输入框的抖动效果
- [cocos2d-x] --- 实现Dota技能冷却效果
- js 鼠标放图片上抖动效果
- 分享一段图片抖动放大效果CSS3代码
- cocos2d 动画效果
- cocos2d-JS 实现 横 竖 屏 显示提示图片效果 !!!!!
- 源码分析使用Cocos2d-x实现2D光线效果
- 使用UICollectionView实现应用程序管理(删除图标 抖动效果)
- 零基础学习IOS开发(三)- cocos2d-x3.0 显示图像及基本动画效果
- Cocos2d-x 自制提示框(就是游戏登陆失败之类的提示一下消失效果)
- Cocos2d-x能够实现的效果总结 (2013-09-03 15:41:47)