Cocos2d-x 抖动效果
来源:互联网 发布:隐藏游戏的软件 编辑:程序博客网 时间:2024/05/16 13:00
在网上看到一个挺有意思的东西 -- 抖动效果。这个动作应该可以用在制作战争类游戏炸弹爆炸或者渲染游戏画面效果等。
这是一个简单的动作,其实如果自己实现的话也是不难的,想一下都知道:所谓抖动,那么就是不停地在一个范围内变换位置,最后还原到原来的位置。
而我所看到网上贴出来的代码其实也是这样实现的。
下面贴出抖动效果CCShake这个动作的源码(代码中已经注释的很清楚了):
#ifndef __SHAKE_H__#define __SHAKE_H__#include "cocos2d.h"USING_NS_CC;class CCShake : public cocos2d::CCActionInterval{public:CCShake(); //下面给出了两个创建该抖动动作实例的类方法: //①参数指定动作执行时间和抖动范围(x和y相同)// Create the action with a time and a strength (same in x and y)static CCShake *create(float d, float strength ); //②参数指定动作执行时间和x,y抖动范围// 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 actionfloat m_strength_x, m_strength_y;};#endif //__SHAKE_H__
#include "CCShake.h"#include "cocos2d.h"USING_NS_CC;// not really useful, but I like clean default constructorsCCShake::CCShake() : m_strength_x(0), m_strength_y(0){}CCShake *CCShake::create( float d, float strength ){// call other construction method with twice the same strengthreturn 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 includedstatic 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 positionm_pTarget->setPosition( ccpAdd(m_StartPosition, ccp( randx, randy)));}void CCShake::startWithTarget(CCNode *pTarget){CCActionInterval::startWithTarget( pTarget );// save the initial positionm_StartPosition=pTarget->getPosition();}void CCShake::stop(void){// Action is done, reset clip positionthis->getTarget()->setPosition( m_StartPosition);CCActionInterval::stop();}
用法也是很简单
CCShake* shake = CCShake::create(3, 15); pSprite->runAction(shake);
参考文章:http://blog.justbilt.com/772/
- Cocos2d-x 抖动效果
- COCOS2D-X 抖动效果 CCShake
- COCOS2D-X 抖动效果 CCShake
- COCOS2D-X 抖动效果 CCShake
- cocos2d-x (3.0)抖动效果
- cocos2d-x-3.3rc0退出抖动问题
- cocos2d-x橡皮擦效果---
- cocos2d-x 手电筒效果
- Cocos2d-x粒子效果
- cocos2d-x 手电筒效果
- cocos2d-x 效果
- cocos2d-x 粒子效果
- cocos2d-x橡皮擦效果---
- cocos2d-x button 效果
- 抖动效果
- 抖动效果
- 抖动效果
- 抖动效果
- lua中的table的升序和降序
- 详解Android SurfaceFinger服务
- GDI实现图像的简单显示特效
- Java学习第一课 java概述
- Windows窗口消息大全
- Cocos2d-x 抖动效果
- Android杂谈--Activity、Window、View的关系
- 5-Step搭建博客
- 关于grep匹配多行的补充
- qtp 11.5链接oracle测试
- 求并联后的阻值
- 模糊查询Like与Instr
- Android 签名详解
- 10004--Bicoloring