Cocos2d-x beginner's Guide (3): Expanding CCAction to make your own motion track

Source: Internet
Author: User

There are many kinds of Basic Motion Animation in the cocos2d-x example, but still don't feel satisfied with it. Fortunately, this is open-source. We can refer to his source code to implement an action on our own.

First, take CCMoveTo, which is the most familiar one. First look at the source code.

[Cpp]
Class CC_DLL CCMoveTo: public CCActionInterval
{
Public:
/** Initializes the action statement */
[Cpp]
Bool initWithDuration (ccTime duration, const CCPoint & position );
 
Virtual CCObject * copyWithZone (CCZone * pZone );
Virtual void startWithTarget (CCNode * pTarget );
Virtual void update (ccTime time );
 
Public:
/** Creates the action */
Static CCMoveTo * actionWithDuration (ccTime duration, const CCPoint & position );
 
Protected:
CCPoint m_endPosition;
CCPoint m_startPosition;
CCPoint m_delta;
};

Is it very concise? Let's take a look at the implementation process of each function. Generally, we use

[Cpp]
Static CCMoveTo * actionWithDuration (ccTime duration, const CCPoint & position );
Let's look at its internal structure.

[Cpp]
CCMoveTo * CCMoveTo: actionWithDuration (ccTime duration, const CCPoint & position)
{
CCMoveTo * pMoveTo = new CCMoveTo ();
PMoveTo-> initWithDuration (duration, position );
PMoveTo-> autorelease ();
 
Return pMoveTo;
}
Obviously, he first instantiated it and then initialized it. Then let's go and see it.

 
[Cpp]
Bool CCMoveTo: initWithDuration (ccTime duration, const CCPoint & position)
{
If (CCActionInterval: initWithDuration (duration ))
{
M_endPosition = position;
Return true;
}
 
Return false;
}

Let's take a look at the initialization of the parent class. You just need to pass in the time and record our parameters.

Then the main character is coming. The most important thing is the update () function, which is where our actions change. Let's take a look.

Void CCMoveTo: update (ccTime time)
{
If (m_pTarget)
{
M_pTarget-> setPosition (ccp (m_startPosition.x + m_delta.x * time,
M_startPosition.y + m_delta.y * time); // that is, m_delta is the total coordinate width.
}
}

Void CCMoveTo: startWithTarget (CCNode * pTarget)
{
CCActionInterval: startWithTarget (pTarget );
M_startPosition = pTarget-> getPosition ();
M_delta = ccpSub (m_endPosition, m_startPosition); // m_delta obtained here is then in update
}

Now let's make our own path.

[Cpp]
# Include "CCActionInterval. h"
Using namespace cocos2d;
Class myAction: public CCActionInterval
{
Public:
/** Initializes the action */
Bool initWithDuration (ccTime duration, const CCPoint & position );
 
Virtual void startWithTarget (CCNode * pTarget );
Virtual void update (ccTime time );
 
Public:
/** Creates the action */www.2cto.com
Static myAction * actionWithDuration (ccTime duration, const CCPoint & position );
 
Protected:
CCPoint m_endPosition;
CCPoint m_startPosition;
};

[Cpp]
# Include "myAction. h"
# Include "CCPointExtension. h"
Void myAction: startWithTarget (CCNode * pTarget ){
CCActionInterval: startWithTarget (pTarget );
M_startPosition = pTarget-> getPosition (); // obtain the starting position.
}
 
MyAction * myAction: actionWithDuration (ccTime duration, const CCPoint & position ){
MyAction * pmyAction = new myAction ();
PmyAction-> initWithDuration (duration, position );
PmyAction-> autorelease ();
 
Return pmyAction;
}
// This function obtains the initialization data.
Bool myAction: initWithDuration (ccTime duration, const CCPoint & position ){
If (CCActionInterval: initWithDuration (duration ))
{
M_endPosition = position; // the center of the circle is obtained here, that is, where to go
Return true;
}
 
Return false;
}
Void myAction: update (ccTime time ){
If (m_pTarget)
{
// The time here is equivalent to the percentage of the current time in the total required running time
/** Here, the trajectory uses a circle.
X = sin (t) y = (sin (t), when the time is finished, it is a circle, 2*180 = 360 degrees
*/
Float tempx = (m_startPosition.x-m_endPosition.x) * (m_startPosition.x-m_endPosition.x );
Float tempy = (m_startPosition.y-m_endPosition.y) * (m_startPosition.y-m_endPosition.y );
Float r = sqrt (tempx + tempy );
Float x = r * sin (2*3.14 * time );
Float y = r * cos (2*3.14 * time );
M_pTarget-> setPosition (ccpAdd (m_startPosition, ccp (x, y )));
}
}

Usage:

Add # include "myAction. h" to the class to be used"

CCFiniteTimeAction * action5 = myAction: actionWithDuration (15, ccp (x + 250, y + 150 ));

In this way, you can call

BirdSpite-> runAction (action5 );

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.