CCAction运动中没有实现椭圆运动或者圆周运动,于是在网上找了很多相关帖子,有一个CCCircleBy可以实现圆周运动,但是当使用CCRepeatForever循环运动时,总是会卡一下,所以在网上有找了一个实现椭圆运动的例子,当长半轴和短半轴相等时,就是圆周运动,这个循环运动倒是不卡。
#include "../actions/CCActionInterval.h"
//包含系统延时类动作头文件
#define PI 3.14159
typedef struct _lrTuoyuanConfig{
CCPoint centerPosition; //中心点坐标
float aLength; //长半轴
float cLength; //椭圆c长,也就是半焦距
}lrTuoyuanConfig;
class __declspec(dllexport) LRTuoyuanBy : public CCActionInterval
{
public:
bool initWithDuration(float f, const lrTuoyuanConfig& c);
virtual void update(float time);
public:
static LRTuoyuanBy* actionWithDuration(float t, const lrTuoyuanConfig& c);
static inline float tuoyuanXat(float a, float bx, float c, float t)
{
return -a*cos(2*PI*t)+a;
}
static inline float tuoyuanYat(float a, float by, float c, float t)
{
float b = sqrt(powf(a,2)-powf(c,2));
return b*sin(2*PI*t);
}
protected:
lrTuoyuanConfig m_sConfig;
CCPoint m_startPosition;
CCPoint s_startPosition;
};
//实现文件为:
LRTuoyuanBy* LRTuoyuanBy::actionWithDuration(float t, const lrTuoyuanConfig& c)
{
LRTuoyuanBy* pTuoyuanBy = new LRTuoyuanBy();
pTuoyuanBy->initWithDuration(t,c);
pTuoyuanBy->autorelease();
return pTuoyuanBy;
}
bool LRTuoyuanBy::initWithDuration(float t, const lrTuoyuanConfig& c)
{
if(CCActionInterval::initWithDuration(t))
{
m_sConfig = c;
return true;
}
return false;
}
void LRTuoyuanBy::update(float time)
{
if(m_pTarget)
{
CCPoint s_startPosition = m_sConfig.centerPosition;
//中心点坐标
float a = m_sConfig.aLength;
float bx = m_sConfig.centerPosition.x;
float by = m_sConfig.centerPosition.y;
float c = m_sConfig.cLength;
float x = tuoyuanXat(a, bx, c, time); //调用之前的坐标计算函数来计算出坐标值
float y = tuoyuanYat(a, by, c, time);
m_pTarget -> setPosition(ccpAdd(s_startPosition, ccp(x-a, y)));
//由于我们画计算出的椭圆以原点为中心,所以要加上原来设定的中心点坐标
}
}
下面看看怎样使用
首先定义一个lrTuoyuanConfig
lrTuoyuanConfig config;
config.centerPosition = ccp(240,160);
config.aLength = 100;
config.cLength = 60;
sprite -> runAction(CCRepeatForever::create(LRTuoyuanBy::actionWithDuration(1.0f, config));
这样就可以让精灵实现无限椭圆运动
当cLength = 0时,就是圆周运动