Cocos2d-x Learning (2): Action (delayed Action)

Source: Internet
Author: User

Most of the genie in the game is dynamic, so the category is an indispensable part of the game engine, today is a simple record of the Cocos2d-x commonly used category.

 


Cocos2d-x provides a lot of basic classification classes, mainly including two categories: one is the instantaneous action (CCActionInstant), the other is the delay action (CCActionInterval), today mainly record the general usage of the delay classification class.

 


1. The basic delay Action provided by the Cocos2d-x provides ActionTo and ActionBy: (Action refers to a variety of actions)

ActionTo refers to the end of an action, that is, the end of the action.

ActionBy refers to the action execution status, that is, the action execution process.

 


Common latency classes include:

Mobile action: CCMoveBy CCMoveTo

Rotation action: CCRotateBy CCRotateTo

Scaling action: CCScaleBy CCScaleTo

 

Hop action: CCJumpBy CCJumpTo

Fade in and fade out action: CCFadeBy CCFadeIn CCFadeOut


Besell curve action: CCBezierBy CCBezierTo

... (There are many other types, so I won't list them one by one. For details, see)

 


2. General Usage

The simplest action is to move data. The following uses CCMoveBy and CCMoveTo as an example. The code is very simple.


[Cpp]
Void HelloWorld: spriteMove ()
{
CCSize winSize = CCDirector: sharedDirector ()-> getWinSize ();
 
CCActionInterval * actionTo = CCMoveTo: actionWithDuration (2.0f,
CCPointMake (winSize. width * 3/4, winSize. height/2 ));
CCActionInterval * actionBy = CCMoveBy: actionWithDuration (2.0f,
CCPointMake (-winSize. width/2, 0 ));
CCActionInterval * actionByCopy = (CCActionInterval *) actionBy-> copy ();
CCActionInterval * actionBack = actionBy-> reverse ();
 
OldManSprite-> runAction (CCSequence: actions (actionTo, actionByCopy, NULL ));
YoungSisterSprite-> runAction (CCSequence: actions (actionBy, actionBack, NULL ));
}
Void HelloWorld: spriteMove ()
{
CCSize winSize = CCDirector: sharedDirector ()-> getWinSize ();

CCActionInterval * actionTo = CCMoveTo: actionWithDuration (2.0f,
CCPointMake (winSize. width * 3/4, winSize. height/2 ));
CCActionInterval * actionBy = CCMoveBy: actionWithDuration (2.0f,
CCPointMake (-winSize. width/2, 0 ));
CCActionInterval * actionByCopy = (CCActionInterval *) actionBy-> copy ();
CCActionInterval * actionBack = actionBy-> reverse ();

OldManSprite-> runAction (CCSequence: actions (actionTo, actionByCopy, NULL ));
YoungSisterSprite-> runAction (CCSequence: actions (actionBy, actionBack, NULL ));
}

The usage is simple. The CCPoint parameter in CCMoveTo is the location point to be "Moved", and the CCPoint parameter in CCMoveBy is the offset www.2cto.com corresponding to the x and Y axes respectively.

(Here are some modifications to the test example in the cocos2d-x)


Note that actionBy cannot be used repeatedly. If it is used for the second time, it will continue from the position where it was used for the first time. If you do not understand it, try it. The phenomenon is obvious!

 

 

3. Callback for completed actions

Generally, three callback methods are supported.

(1). No sender and no data callback: CCCallFunc. Example:


[Cpp]
Void HelloWorld: actionCallback ()
{
CCSize winSize = CCDirector: sharedDirector ()-> getWinSize ();
 
CCLabelTTF * label = CCLabelTTF: labelWithString ("Action Callback! "," Marker Felt ", 32 );
Label-> setPosition (ccp (winSize. width/2, winSize. height/2 ));
 
This-> addChild (label );
}
Void HelloWorld: actionCallback ()
{
CCSize winSize = CCDirector: sharedDirector ()-> getWinSize ();

CCLabelTTF * label = CCLabelTTF: labelWithString ("Action Callback! "," Marker Felt ", 32 );
Label-> setPosition (ccp (winSize. width/2, winSize. height/2 ));

This-> addChild (label );
}


(2). A sender has no data callback: CCCallFuncN (N is CCNode). The example is as follows:


[Cpp]
Void HelloWorld: actionCallbackN (CCNode * pSender)
{
CCSprite * sprite = (CCSprite *) pSender;
 
// The callback turns red.
Sprite-> setColor (ccRED );
}
Void HelloWorld: actionCallbackN (CCNode * pSender)
{
CCSprite * sprite = (CCSprite *) pSender;

// The callback turns red.
Sprite-> setColor (ccRED );
}


(3). Some senders have data callbacks: CCCallFuncND (N is CCNode, D is data, is void * type)


[Cpp]
Void HelloWorld: actionCallbackND (CCNode * pSender, void * data)
{
CCSize winSize = CCDirector: sharedDirector ()-> getWinSize ();
 
CCSprite * sprite = (CCSprite *) pSender;
 
// The callback turns blue.
Sprite-> setColor (ccBLUE );

// Receives callback data
CCLabelTTF * label = CCLabelTTF: labelWithString (char *) data, "Marker Felt", 32 );
Label-> setPosition (ccp (winSize. width/2, winSize. height/2 ));
This-> addChild (label );
 
CCLog (char *) data );
}
Void HelloWorld: actionCallbackND (CCNode * pSender, void * data)
{
CCSize winSize = CCDirector: sharedDirector ()-> getWinSize ();

CCSprite * sprite = (CCSprite *) pSender;

// The callback turns blue.
Sprite-> setColor (ccBLUE );
 
// Receives callback data
CCLabelTTF * label = CCLabelTTF: labelWithString (char *) data, "Marker Felt", 32 );
Label-> setPosition (ccp (winSize. width/2, winSize. height/2 ));
This-> addChild (label );

CCLog (char *) data );
}
Example of using callback:


[Cpp]
Void HelloWorld: actionListen ()
{
CCSize winSize = CCDirector: sharedDirector ()-> getWinSize ();
 
CCFiniteTimeAction * action = CCSequence: actions (
CCMoveBy: actionWithDuration (2.0f, CCPointMake (winSize. width/2, 0 )),
CCCallFunc: actionWithTarget (this, callfunc_selector (HelloWorld: actionCallback), NULL );
 
CCFiniteTimeAction * actionN = CCSequence: actions (
CCMoveBy: actionWithDuration (2.0f, CCPointMake (-winSize. width/2, 0 )),
CCCallFuncN: actionWithTarget (this, callfuncN_selector (HelloWorld: actionCallbackN), NULL );
 
CCFiniteTimeAction * actionND = CCSequence: actions (
CCMoveBy: actionWithDuration (2.0f, CCPointMake (winSize. width/2, 0 )),
CCCallFuncND: actionWithTarget (this, callfuncND_selector (HelloWorld: actionCallbackND), (void *) "Callback Data"), NULL );
 
OldManSprite-> runAction (actionND );
YoungSisterSprite-> runAction (actionN );
}
Void HelloWorld: actionListen ()
{
CCSize winSize = CCDirector: sharedDirector ()-> getWinSize ();

CCFiniteTimeAction * action = CCSequence: actions (
CCMoveBy: actionWithDuration (2.0f, CCPointMake (winSize. width/2, 0 )),
CCCallFunc: actionWithTarget (this, callfunc_selector (HelloWorld: actionCallback), NULL );

CCFiniteTimeAction * actionN = CCSequence: actions (
CCMoveBy: actionWithDuration (2.0f, CCPointMake (-winSize. width/2, 0 )),
CCCallFuncN: actionWithTarget (this, callfuncN_selector (HelloWorld: actionCallbackN), NULL );

CCFiniteTimeAction * actionND = CCSequence: actions (
CCMoveBy: actionWithDuration (2.0f, CCPointMake (winSize. width/2, 0 )),
CCCallFuncND: actionWithTarget (this, callfuncND_selector (HelloWorld: actionCallbackND), (void *) "Callback Data"), NULL );

OldManSprite-> runAction (actionND );
YoungSisterSprite-> runAction (actionN );
}
Conclusion: You need to define the callback function based on your needs.

If it is just a simple notification, use CCCallFunc; if you need to know the sender information, use CCCallFuncN; if you need to include some data information, use CCCallFuncND

Author: oneRain88

 

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.