Cocos2d-x Action notes

Source: Internet
Author: User

Cocos2d-x Action notes


Basic Concepts

1. CCAction is the base class of the category class. Therefore, all actions are derived from this class. It creates an object to represent an action. Actions Act on CCNode, so any action is executed by CCNode.

Instance 1. One image is moved from one second to another

Size visibleSize = Director: getInstance ()-> getVisibleSize ();
Vec2 origin = Director: getInstance ()-> getVisibleOrigin ();

Auto sprite02 = Sprite: create ("yu.png ");
Sprite02-> setPosition (Point (visibleSize. width/2, visibleSize. height/2 ));
This-> addChild (sprite02 );

// Simple movement to a specified point
Auto action = MoveTo: create (1.0f, Point (0, 0 ));
Sprite02-> runAction (action );

2. as a base class, CCAction is essentially an interface (that is, an abstract class). Most of its implementation classes are derived from CCFinteTimeAction, which defines the actions that can be completed within a limited time. CCFinteTimeAction mainly acts on CCActioninstant and CCActionIntervel ).

2.1 instantaneous action (CCActionInstant)

2.1.1 Place the node to the screen coordinate (100,100) during the CCPlace action, and then execute the curve motion action1.

Size visibleSize = Director: getInstance ()-> getVisibleSize ();
Vec2 origin = Director: getInstance ()-> getVisibleOrigin ();

Auto sprite02 = Sprite: create ("yu.png ");
Sprite02-> setPosition (Point (visibleSize. width/2, visibleSize. height/2 ));
This-> addChild (sprite02 );

// The instantaneous action specifies the location for curve motion to the besell Curve
CcBezierConfig bezierCon;
BezierCon. controlPoint_1 = CCPointMake (200,150); // Control Point 1
BezierCon. controlPoint_2 = CCPointMake (200,460); // Control Point 2
BezierCon. endPosition = CCPointMake (340,100); // end position
Auto * placeAction = Place: create (Point (visibleSize. width/2, visibleSize. height/2 ));
// CCActionInterval * curveMove = BezierTo: create (2, bezierCon );
CCActionInterval * curveMove = CCBezierBy: create (2, bezierCon); // supports reverse
CCActionInterval * action1 = curveMove-> reverse (); // reverse inverse Action Method
Auto * action = Sequence: create (placeAction, action1, NULL ); // The Sequence action Sequence is a composite action that accepts multiple actions during initialization. When executed, the actions are executed sequentially.
Sprite02-> runAction (action );


2.2.2 when the CCFlipX and CCFlipY actions are performed, the genie is displayed in reverse direction along the x and Y axes, when a fish moves to the boundary, this action will take a 180-degree rotation of the fish and need to be written by itself. It just rotates the fish image. I feel this kind of action notes

CCFiniteTimeAction * flipXAction = CCFlipX: create (true );
Auto curveMove = MoveTo: create (1.0f, Point (200,20 ));
CCAction * action = Sequence: create (curveMove, flipXAction, curveMove-> reverse (), NULL );
Sprite02-> runAction (action );

2.2.3 The CCShow and CChide actions are used to show and hide nodes respectively. They act the same way as setting the Visible attribute of nodes.

CCFiniteTimeAction * hideAction = CCHide: create ();
Auto curveMove = MoveTo: create (1.0f, Point (300,300 ));
CCAction * action = Sequence: create (curveMove, hideAction, NULL );
Sprite02-> runAction (action );

2.2.4 The CCCallFunc series of actions include CCCallFunc, CCCallFuncN, CCCallFuncND, and CCCallFuncO, which are used to call methods in the action.

CCCallFunc has no parameters

CCCallFuncN 1 parameter CCNode * type

CCCallFuncND two parameters: CCNode * and custom Parameters

CCCallFuncO 1 parameter CCObject *

Example:

Auto curveMove = MoveTo: create (1.0f, Point (300,300 ));
Auto * actionMoveDone = CCCallFunc: create (this, callfunc_selector (HelloWorld: moveActionEnd ));
CCAction * action = CCSequence: create (curveMove, actionMoveDone, NULL );
Sprite02-> runAction (action );

2.3 sustained action

Each continuous action has two different variant actions, namely, the absolute change of the attribute value of the node with the suffix "To" and the relative change of the attribute value with the suffix ".

Different actions can be divided into four categories: location change actions, attribute change actions, visual special effects actions, and control actions;

2.3.1 location change action

A, CCMoveTo and CCMoveBy: nodes perform linear motion. After the time and end point of the action are set, the node moves from the current position to the end point within the specified time.

B, CCJumpTo and CCJumpBy: the node jumps to the specified position with a certain trajectory

// Continuous actions: CCJumpTo and CCJumpBy
CCAction * action = JumpTo: create (1.0f, Point (500,500 );
C, CCBezierTo and CCBezierBy

CcBezierConfig besuppliers;
Bezr. controlPoint_1 = Point (20,150 );
Bezr. controlPoint_2 = Point );
Bezr. endPosition = Point );
CCFiniteTimeAction * beizeAction = CCBezierTo: create (2, bezr );
Sprite02-> runAction (beizeAction );

2.3.2 attribute change action

A, CCScaleTo and CCScaleBy generate scaling effect auto action = CCScaleTo: create (2.0f, 4 );

B, CCRotateTo and CCRotateBy Rotation effects auto action2 = CCRotateTo: create (2.0f, 90 );

C, CCFadeIn and CCFadeOut generate fade-in and fade-out Results auto action3 = CCFadeIn: create (2.0f );

D, CCFadeTo is used to set the transparency for a period of time auto action7 = CCFadeTo: create (2.0f, 125 );

E, CCTintTo and CCTintBy set the color variation auto action6 = CCTintTo: create (2.0f, 0,255,255 );

2.3.3 Visual Effects

A. CCBlink causes the target node to flash

Auto blink = CCBlink: create (2.0f, 5 );

B. Play Frame Animation with CCAnimation

2.3.4 control action

Such actions include CCDelayTime, CCRepeat, and CCRepeatForever.

A, CCDelayTime can delay the action time for a certain time

B. CCRepeat can repeat existing actions for a certain number of times.

C. CCRepeateForever can keep an action repeated.

3. compound action

A. Repeated action (CCRepeat/CCRepeatForever)

Auto curveMove = MoveTo: create (2.0f, Point (100,100 ));
Auto rotateTo = CCRotateTo: create (1.0f, 180 );
Auto back = MoveTo: create (2.0f, Point (visibleSize. width/2, visibleSize. height/2 ));
Auto action = Sequence: create (rotateTo, curveMove, rotateTo, back, NULL );
Auto repeat = CCRepeat: create (action, 2 );
Sprite02-> runAction (repeat );

B. Coordinate action CCSpawn

// Parallel action
Auto curveMove = MoveTo: create (2.0f, Point (100,100 ));
Auto rotateTo = CCRotateTo: create (2.0f, 180 );
Auto spawn1 = CCSpawn: create (curveMove, rotateTo, NULL );
Sprite02-> runAction (spawn1 );

C, sequence CCSequence

D. The delayed action CCDelayTime is used together with the sequence.

4. Variable Speed

A. CCSpeed is used to linearly change the speed of an action.

Auto curveMove = MoveTo: create (2.0f, Point (visibleSize. width-30, visibleSize. height/2 ));
CCSpeed * speed = CCSpeed: create (curveMove, 5.0f );
Sprite02-> runAction (speed );

// The tag can be obtained later.

Speed-> setSpeed (2.0f );

B. CCActionEase speed buffering and speed change

The CCActionEase series includes 15 actions, which can be summarized as 5 actions: exponential buffering, sine buffering, elastic buffering, jump buffering, and response buffering. Each type of action has three transformations In different periods: In, Out, And InOut.

Specified buffer: easeInExpo, easeOutExpo, and easeInOutExpo

Sine Buffer: easeInSine, easeOutSine, easeInOutSine

Elastic buffering: easeInElastic, easeOutElastic, easeInOutElastic

Hop Buffer: easeInBounce, easeOutBounce, easeInOutBounce

Response Buffer: caseInBack, easeOutBack, easeInOutBack


Auto curveMove = MoveBy: create (2.0f, Point (visibleSize. width-30, visibleSize. height/2 ));
CCEaseSineIn * sineIn = CCEaseSineIn: create (curveMove );
Sprite02-> runAction (sineIn );

The By action must be used as the buffer action.


Make the action look smoother and smoother

Arrival action: starts from fast to slow, stops slowly after fast entry, and gives the player enough visual vision to clearly distinguish the incoming image before the stop

Appearance action: Slow down and fast. The appearance trend and direction are displayed. The screen is removed quickly without being dragged.

Collapse menu

CCMoveTo * move = CCMoveTo: create (0.5f, Point (visibleSize. width/2, visibleSize. height/2 ));
CCAction * action = CCEaseExponentialIn: create (move );
Menu-> runAction (action );


When CCScheduler updates each frame, the update method registered by CCActionManager is triggered.












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.