The cocos2d-x2.2 realizes the elliptical motion, the parameters are the central point coordinate and the long half axis, the short half axis
CCAction motion does not implement elliptical motion or circular motion. Therefore, I have found many related posts on the Internet. One CCCircleBy can achieve circular motion. However, when CCRepeatForever is used for circular motion, it always gets stuck, so I found an example on the Internet to implement elliptical motion. When the long half axis is equal to the short half axis, it is a circular motion, which is not stuck.
# Include "../actions/CCActionInterval. h" // contains the system latency action header file
# Define PI 3.14159
Typedef struct _ lrTuoyuanConfig {
CCPoint centerPosition; // Center Coordinate
Float aLength; // long Half Axis
Float cLength; // elliptical c length, that is, the half focal length
} 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) +;
}
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;
};
// The implementation file is:
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; // Center Coordinate
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); // call the previous coordinate calculation function to calculate the coordinate value.
Float y = tuoyuanYat (a, by, c, time );
M_pTarget-> setPosition (ccpAdd (s_startPosition, ccp (x-a, y); // Since the calculated ellipse is centered on the origin, therefore, we need to add the originally set central point coordinates.
}
}
Next let's take a look at how to use
First define an lrTuoyuanConfig
LrTuoyuanConfig config;
Config. centerPosition = ccp (240,160 );
Config. aLength = 100;
Config. cLength = 60;
Sprite-> runAction (CCRepeatForever: create (LRTuoyuanBy: actionWithDuration (1.0f, config ));
This allows the genie to achieve infinite elliptical motion.
When cLength = 0, it is a circular motion.