CCAction and its subclass play this role. It is implemented by changing the attributes of these entities. below is its class structure diagram.
From the structure diagram, we can see that the base class of the category class is CCAction, and many actions can be implemented by inheriting it. CCFiniteTimeAction: a limited action execution class, that is, to execute a series of actions in chronological order, after which the action ends; CCSpeed: Adjust the execution speed of the entity (node); CCFollow: allows a node to move along with another specified node. Next we will mainly learn CCFiniteTimeAction, which is common in normal development.
CCFiniteTimeAction is divided into CCActionInstanse (base class of instantaneous action) and CCActionInterval (base class of delayed action ). CCActionInstanse: There is nothing special. The main difference with CCActionInterval is that there is no execution process, and the action is executed instantly. CCActionInterval: The execution takes a certain amount of time (or a process ). What we use most is the delayed action. Next we will introduce it separately.
According to the above class structure, there are many CCActionInterval sub-classes, you can learn through the tests example of the cocos2d-x, mainly have these actions: Moving (CCMoveTo/CCMoveBy), scaling (CCScaleTo/CCScaleBy) CCRotateTO/CCRotateBy, CCSkewTo/CCSkewBy, CCJumpTo/CCJumpBy, and CCBezierTo/CCBezierBy), fade in and fade out (CCFadeIn/CCFadeOut), dyeing (CCTintTo/CCTintBy), and so on, you can also combine the above actions into a sequence. The following is a sample code for the movement and scaling actions, other actions are similar, are to call the actionWithDuration function, when the use of the specific parameter meaning can refer to the tests built-in cocos2d-x example.
CCMoveTo and CCMoveBy
The difference between the two is that CCMoveTo is the moving to the specified coordinate, and CCMoveBy is the relative coordinate. For example, ccp (50, 50), the former indicates moving to x = 50, y = 50, and the latter indicates moving 50 units to x and moving 50 units to y. The instance code is as follows:
[Cpp]
CCSize winSize = CCDirector: sharedDirector ()-> getWinSize ();
CCActionInterval * actionMoveTo = CCMoveTo: actionWithDuration (2, ccp (winSize. width-30, winSize. height-30 ));
CCActionInterval * actionMoveBy = CCMoveBy: actionWithDuration (1, ccp (-50,-50 ));
// M_Soldier-> runAction (actionMoveTo );
// M_Soldier-> runAction (actionMoveBy );
M_Soldier-> runAction (CCSequence: actions (actionMoveTo, actionMoveBy, NULL ));
CCSequence is the action sequence. It combines several actions in order and then executes them in sequence. NULL indicates the end mark. The code above indicates that the genie moves to the upper right corner of the screen, and then moves 50 units along the negative direction of the X axis and Y axis.
CCScaleTo and CCScaleBy
Zooming. The former indicates zooming to xx times; the latter indicates zooming to xx times. You can specify the horizontal and vertical scaling multiples. The instance code is as follows:
[Cpp]
CCActionInterval * actionScaleTo = CCScaleTo: actionWithDuration (1, 0.6f );
CCActionInterval * actionScaleTo2 = CCScaleTo: actionWithDuration (1, 0.6f, 1.5f );
CCActionInterval * actionScaleBy = CCScaleBy: actionWithDuration (1, 0.4f );
CCActionInterval * actionScaleBy2 = CCScaleBy: actionWithDuration (1, 0.4f, 2.0f );
M_Soldier-> runAction (actionScaleTo );
TempSoldier-> runAction (CCSequence: actions (actionScaleBy2, actionScaleBy2-> reverse (), NULL ));
Reverse () indicates the inverse action of the action, that is, it is restored to its original state.
Next, let's take a look at CCSpawn, CCRepeatForever, CCRepeat, and CCDelayTime.
CCSpawn: Unlike CCSequence, it means to combine several actions and execute them simultaneously (in parallel). The execution time is based on the longest action. The following code indicates that the sprite rotates and jumps four times in two seconds. The height of each hop is 50 units, and the distance between 300 units is jumped along the X axis; rotate 720 degrees.
[Cpp]
CCAction * action = CCSpawn: actions (
CCJumpBy: actionWithDuration (2, CCPointMake (, 0), 50, 4 ),
CCRotateBy: actionWithDuration (2,720 ),
NULL );
M_grossini-> runAction (action );
CCRepeatForever: indicates the infinite repetition of an action or action sequence.
CCRepeat: Indicates repeated execution of an action or action sequence, but a limited number of repetitions. You can specify the number of repetitions.
[Cpp] www.2cto.com
CCActionInterval * act1 = CCRotateTo: actionWithDuration (1, 90 );
CCActionInterval * act2 = CCRotateTo: actionWithDuration (1, 0 );
CCActionInterval * seq = (CCActionInterval *) (CCSequence: actions (act1, act2, NULL ));
CCAction * rep1 = CCRepeatForever: actionWithAction (seq );
CCActionInterval * rep2 = CCRepeat: actionWithAction (CCFiniteTimeAction *) (seq-> copy ()-> autorelease (), 10 );
M_tamara-> runAction (rep1 );
M_kathia-> runAction (rep2 );
CCDelayTime: indicates the delay. An interval is added between actions. The following code indicates that after the genie executes a move action, it suspends the operation for 2 seconds and then continues to execute the subsequent move action.
[Cpp]
CCActionInterval * move = CCMoveBy: actionWithDuration (1, CCPointMake (150,0 ));
CCFiniteTimeAction * action = CCSequence: actions (move, CCDelayTime: actionWithDuration (2), move, NULL );
M_grossini-> runAction (action );