Cocos2d-x Study Notes (6) CCAction Analysis

Source: Internet
Author: User

Cocos2d-x Study Notes (6) CCAction Analysis

 

 

Preface
I don't know if you are in the same mood as me. I finally waited for the CCAction to come out. If CCSprite is the body, then CCAction must be the soul. Its combination makes the entire game dynamic. Of course, it is not the only way to be dynamic.

 

Source code analysis
Class CC_DLL CCAction: public CCObject {public: CCAction (void); virtual ~ CCAction (void); const char * description (); // virtual CCObject * copyWithZone (CCZone * pZone); // whether the action is completed virtual bool isDone (void ); // called before the action starts. It sets a target virtual void startWithTarget (CCNode * pTarget); // It is called after the action is completed and the target is set to null. Note: Do not call this method. Replace 10 target-> stopAction (action) virtual void stop (void); // call each frame. Note: Do not refactor it unless you make sure that you are doing virtual void step (float dt);/*** each frame is called once, and time is a 0 ~ Value Between 1 ** For example: *-0 indicates that the action has just started *-0.5 indicates that the action is in the middle *-1 indicates that the action has ended **/virtual void update (float time ); // get/set the target inline CCNode * getTarget (void) {return m_pTarget;} inline void setTarget (CCNode * pTarget) {m_pTarget = pTarget ;} // get/set the initial target of the action (usually not set) inline CCNode * getOriginalTarget (void) {return m_pOriginalTarget;} inline void setOriginalTarget (CCNode * pOriginalTarget) {m_pOriginalTarget = pOriginalTarget;} // obtain/set the Tag inline int getTag (void) {return m_nTag;} inline void setTag (int nTag) {m_nTag = nTag;} public: // create an Action static CCAction * create (); protected: CCNode * m_pOriginalTarget; CCNode * m_pTarget; int m_nTag ;};

The CCNode subclass level analysis CCAction is the base class of the entire action system. You can use runAction on the CCNode to execute it, not only to complete an independent action, you can also define action sequences, combined actions, and reverse actions.
This figure clearly shows the level system of the entire CCAction. There are three main categories: CCFiniteTimeAction (limited action execution class), CCSpeed (node execution speed class), and CCFollow (nodes follow another node to move ). CCFiniteTimeAction can be divided into CCActionInstant (instantaneous action) and CCActionInterval (delayed action ). The actions we see are generally the latter. The following describes some common delayed actions: 1) CCMoveTo/CCMoveBy move 2) CCRotateTo/CCRotateBy rotate 3) CCSkewTo/CCSkewBy twist 4) CCJumpTo/CCJumpBy skip 5) CCBezierTo/CCBezierBy besell curve 6) CCBink blinking 7) CCScaleTo/CCScaleBy scaling 8) CCFadeIn/CCFadeOut fade-in and fade-out 9) CCTintTo/CCTintBy dyeing all the actions are created using the create Method. For example:
CCMoveTo *move = CCMoveTo::create(2.0f, cpp(50, 50));
If you are careful, you will find that there are two Methods: To and By. The difference between them is that To is the specified coordinate, and By is the relative coordinate.
Before analyzing CCNode, CCActionManager mentioned CCActionManager. It is a singleton for managing all actions. Working Principle 10: When CCNode executes runAction, this function will pass the action to the single instance of CCActionManager through the addAction function of the Action Management class and then add the action to its own action sequence. Generally, 10 does not need this class. We can use stopAction, StopActionByTag, stopAllAction, and other functions in the CCNode class for management, however, there are two special cases where the CCActionManager class is used: 1) the operator of the action is not the same node; 2) when the activity needs to be paused or restarted.
Class CC_DLL CCActionManager: public CCObject {public: CCActionManager (void );~ CCActionManager (void); // Add the Action void addAction (CCAction * pAction, CCNode * pTarget, bool paused) to the specified target; // delete all actions void removeAllActions (void ); // Delete void removeAllActionsFromTarget (CCObject * pTarget); // Delete the specified action void removeAction (CCAction * pAction ); // delete/obtain the specified action void removeActionByTag (unsigned int tag, CCObject * pTarget); CCAction * getActionByTag (unsigned int tag, CCObject * pTarget ); // obtain the number of target actions: unsigned int numberOfRunningActionsInTarget (CCObject * pTarget); // pause the target action void pauseTarget (CCObject * pTarget ); // restore the target action void resumeTarget (CCObject * pTarget); // pause all running actions CCSet * pauseAllRunningActions (); // restore void resumeTargets (CCSet * targetsToResume );...}; // The above is the CCActionManager source code analysis // example of use: pNode-> runAction (CCScaleBy: create (2, 2); CCDirector * pDirector = CCDirector: sharedire (); pDirector-> getActionManager ()-> pauseTarget (pNode); pDirector-> getActionManager ()-> resumeTarget (pNode );

Note: Do not use the action management class easily, unless it is a different action target or pause the restart action.

 

CCRepeat, CCRepeatForeverCCRepeat, and CCRepeatForever are inherited from CCActionInterval, which can be executed multiple times and repeatedly.
// Times indicates the number of pactions executed, while CCRepeat indicates repeated execution until stopstatic CCRepeat * create (CCFiniteTimeAction * pAction, unsigned int times); static CCRepeatForever * create (CCActionInterval * pAction );

The CCSequence and CCSpawnCCSequence classes can execute actions in sequence and allow nodes to execute these actions in sequence.
The CCSpawn class executes several actions at the same time. The duration of the final action is determined by the longest action. For example:
CCSequence: create (move, move_back, NULL); // remember to add NULL at the end of the action sequence

CCReverseTimeCCReverseTime is the reverse execution of an action. It supports the reactionary action sequence. Not all classes support Reactionary Work. XxxTo is usually not supported, and XxxBy is usually supported.

When we do an Action, we need to return the reverse that will use the sprite, But when I need this behavior again, I can use CCReverseTime to achieve this effect. The example code is as follows:

 

CCSprite * spriteTint = CCSprite: create(blocks.png); spriteTint-> setPosition (ccp (size. width/2.0f, size. height/2.0f); this-> addChild (spriteTint, 1); CCActionInterval * forwardBy = CCTintBy: create (4,255, 0, 0 ); CCActionInterval * back = forwardBy-> reverse (); CCReverseTime * reverseTime = CCReverseTime: create (back ); // here, the Action is played in reverse order. CCAction * action = CCSequence: create (forwardBy, back, NULL); spriteTint-> runAction (action );

CCSpeed and CCEaseAction

 

The basic and composite actions implement various attribute changes for CCNode, but the speed of such changes remains unchanged. Through the CCEaseAction and CCSpeed classes, the speed of CCNode execution can be easily modified.

CCSpeed is also a package that can change the execution time of internal actions.

 

CCSpeed *speed = CCSpeed::create(CCMoveBy::create(3, cpp(350, 0)), 2.0f);
CCEaseAction is also a package, but it is generally used to make the action more natural:

 

1) EaseIn // from slow to fast

2) EaseOut // from fast to slow
3) EaseInOut // from slow to fast, and then from fast to slow

4) EaseSineIn // from slow to fast
5) EaseSineOut // from fast to slow
6) EaseSineInOut // from slow to fast, and then from fast to slow

7) EaseExponentialIn // extremely slow
8) EaseExponentialOut // from extremely fast to slow
9) EaseExponentialInOut // slow from extremely fast to slow

You can view the specific differences through debugging.


CCFollowCCFollow allows one node to follow another node for displacement.
static CCFollow *create(CCNode *pFollowNode, const CCRect &rect = CCRectZero);
This method is used to create a CCFollow follow action. You can set a following range so that the following will not be followed when you leave the range. CCFollow is often used to set the Layer to follow sprite, which can achieve the same effect as the camera. Similar examples are also provided in TestCpp.
void ActionFollow::onEnter(){    ActionsDemo::onEnter();    centerSprites(1);    CCSize s = CCDirector::sharedDirector()->getWinSize();    m_grossini->setPosition(ccp(-200, s.height / 2));    CCActionInterval* move      = CCMoveBy::create(2, ccp(s.width * 3, 0));    CCActionInterval* move_back = move->reverse();    CCSequence* seq       = CCSequence::create(move, move_back, NULL);    CCAction* rep               = CCRepeatForever::create(seq);    m_grossini->runAction(rep);    this->runAction(CCFollow::create(m_grossini, CCRectMake(0, 0, s.width * 2 - 100, s.height)));}

A combination of CCRepeat, CCRepeatForever, CCSequence, CCSpawen, CCSpeed, and CCEaseAction is a package container that contains one or more actions to form a specific action, as shown below, we will simulate a heartbeat animation.
CCSprite* pSprite = CCSprite::create(Heart.png);this->addChild(pSprite, 0);auto *scale = CCScaleBy::create(1.0, 1.5);auto *scaleBack = scale->reverse();auto *scaleForever = CCRepeatForever::create(CCSequence::create(scale, scaleBack, NULL));pSprite->runAction(scaleForever);


Related Article

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.