Cocos2d-x 3. x Action
Action is the base class of an Action. All actions are derived from this class. An object created by the Action represents an Action. Actions Act on the Node. Therefore, any action must be executed by the Node object.
// Move an genie from the center of the screen to the position (0, 0) in 3 seconds. auto tortoise = Sprite: create ("other/tortoise.png "); tortoise-> setPosition (Vec2 (visibleSize. width/2 + visibleOrigin. x, visibleSize. height/2 + visibleOrigin. y); this-> addChild (tortoise); auto action = MoveTo: create (3.0f, Vec2 (0 + visibleOrigin. x, 0 + visibleOrigin. y); tortoise-> runAction (action );
An Action can only be used once, because the Action object not only describes the Action, but also stores some intermediate parameters that are constantly changing during the Action persistence process.
As a base class, Action is essentially an interface (abstract class). The implementation class derived from it (such as motion and rotation) is what we actually use. Most of the implementation classes of Action are derived from the FiniteTimeAction subclass of Action, which defines the actions that can be completed within a limited time. FiniteTimeAction defines the reverse method. This method can be used to obtain an action opposite to the original action (inverse action). For example, after hiding an genie, the reverse action is used to display the result. Not all actions have inverse actions. For example, an action that is set to a constant, such as "zoom in", does not have an inverse action, actions with the property set as relative values usually have inverse actions.
Two main classes derived from FiniteTimeAction are instantaneous action ActionInstant and persistent action ActionInterval.
An instantaneous action is an action that can be completed immediately. It is a special case where the duration of an action in FiniteTimeAction is 0. This type of action is the action that will be immediately executed and completed in the next frame, such as setting the position and scaling. This type of action can be simply assigned to the Node, but after packaging them as actions, you can easily combine them with other complex actions.
1. Some common transient actions:
Place
This action is used to place a node to a specified Position. The Position attribute of the modified node is the same.
// Place the genie in the screen coordinate (200,200). auto placeAction = Place: create (Vec2 (200,200); tortoise-> runAction (placeAction );
FlipX and FlipY
These two actions are used to reverse display the Genie along the X and Y axes, respectively. Their functions are the same as setting the FlipX and FlipY attributes of the genie.
// Reverse the sprite along the X axis. auto flipXAction = FlipX: create (true); tortoise-> runAction (flipXAction );
Show and Hide
These two actions are used to show and hide nodes respectively. They act the same way as setting the visible attribute of nodes.
// Hide the genie auto hideAction = Hide: create (); tortoise-> runAction (hideAction );
CallFunc
CallFunc-series actions include _ CCCallFuncND ,__ CCCallFuncO and CallFuncN, which are used to call functions in an action. When an object executes the CallFunc series of actions, it calls a preset method to complete some special functions.
The suffix "N" of the CallFunc series actions indicates the Node parameter, the object that executes the action, and "D" indicates the Data parameter, which indicates the user-defined Data, "O" indicates an Object, which is a User-Defined Object (Ref) parameter.
// Call a function in an action, which can be a class member function or a common function // void fun (Node * node) Common Function void HelloWorld :: fun (Node * node) {CCLOG ("CallFuncN");} // auto callFuncAction = CallFuncN: create (fun); call the common function auto callFuncAction = CallFuncN :: create (this, callfuncN_selector (HelloWorld: fun); // calls the class member function tortoise-> runAction (callFuncAction) in the action );
2. A sustained action is an action completed over a period of time. The vast majority of a sustained action carries a real-type parameter duration used to control the execution time of the action. Each continuous action usually has two different variant actions, with the To and By suffixes: the actions with the suffix To describe the absolute changes of the node attribute values, for example, MoveTo moves an object to a specific position, while the action suffixed with By describes the relative change of the property value. For example, MoveBy moves an object to a relative shift.
Depending on the effect, you can divide the continuous action into four categories: location change action, attribute change action, visual special effect action, and control action.
Location change action
For the position attribute, the engine provides three types of position change actions.
MoveTo and MoveBy are used to make the nodes perform linear motion.
// Within the specified time, the current position is moved straight to the set end position auto moveToAction = MoveTo: create (3.0f, Vec2 (0, 0 )); tortoise-> runAction (moveToAction); // move relative to the current position (100,100) within the specified time. The effect is to move auto moveByAction = MoveBy: create (3.0f, vec2 (100,100); tortoise-> runAction (moveByAction );
JumpTo and JumpBy make the node jump to the specified position with a certain trajectory
// Jump from the current position to the set end position within the specified time. The first parameter indicates the duration, the second parameter indicates the Moving End position, and the third parameter indicates the maximum skip height, the fourth parameter indicates the number of hops auto jumpToAction = JumpTo: create (3.0f, Vec2 (0, 0), 50366f, 5); tortoise-> runAction (jumpToAction ); // compared to the current position (300,300) within the specified time, the result is to jump to the upper right corner to auto jumpByAction = JumpBy: create (3.0f, Vec2 (300,300), 50366f, 5 ); tortoise-> runAction (jumpByAction );
BezierTo and BezierBy make the nodes perform curve motion, and the motion track is described by the besell curve. Each bésel curve contains a starting point and an ending point. In a curve, the start and end points each contain a control point, and the line from the control point to the end point is called the control line. The control line determines the shape of the curve emitted from the endpoint, including two parameters: Angle and length. The angle determines the direction of the curve it controls, that is, the tangent direction of this curve at this control point, the curvature of the length control curve. The longer the control line, the closer the curve it controls to the control line. Any curve can be composed of one or more adjacent besell curves.
// When using the ccBezierConfig struct, you must first create the ccBezierConfig struct, set the endpoint endPosition and the two control points controlPoint_1 and controlPoint_2, and then pass the struct to the ccbezierto or BezierBy initialization method. controlPoint_1 = Vec2 (20,150); bezr. controlPoint_2 = Vec2 (200, 30); bezr. endPosition = Vec2 (160, 30); auto bezierToAction = BezierTo: create (3.0f, bezy); tortoise-> runAction (bezierToAction );
Attribute change action
The animation effect is achieved by changing the attribute value gradually.
ScaleTo and ScaleBy produce Scaling Effect
// The second parameter is the scaling coefficient auto scaleToAction = ScaleTo: create (3.0f, 2); tortoise-> runAction (scaleToAction );
RotateTo and RotateBy produce rotation effect
// The second parameter is angle. The positive direction is clockwise auto rotateToAction = RotateTo: create (3.0f, 30366f); tortoise-> runAction (rotateToAction );
Both FadeIn and FadeOut implement the fade-in effect, while the latter implement the fade-out effect.
FadeTo sets the effect of transparency changes over a period of time
TintTo and TintBy set the tone change
Only nodes that implement the CCRGBAProtocol interface can perform such actions. This is because attributes related to transparency or color are inherited from the CCRGBAProtocol interface. Many common nodes, such as Sprite and LayerColor, implement this interface.
// Create an action with duration and opacity. The second parameter GLubyte is an 8-bit unsigned integer. Therefore, any integer between 0 and can be obtained. transparency-related actions can only be applied to the genie, the child node is not affected by the parent node. Auto fadeToAction = FadeTo: create (3.0f, 100); tortoise-> runAction (fadeToAction); // The rgb value range is 0-255TintTo: create (float duration, GLubyte red, GLubyte green, GLubyte blue); TintBy: create (float duration, GLshort deltaRed, GLshort deltaGreen, GLshort deltaBlue );
Visual Effects
Blink flashes the target node
// Flashes 5 times in 3 seconds. auto blinkAction = Blink: create (3.0f, 5); tortoise-> runAction (blinkAction );
Playing Frame Animation with Animate
Control Action
The control action is used to precisely control the action.
DelayTime continues the action for a period of time.
Repeat repeats existing actions for a certain number of times.
RepeatForever repeats an action.
3. compound action
Combine various basic actions to produce complex actions.
Repeat and RepeatForever
// RepeatAction auto repeatAction = Repeat: create (blinkAction, 3); tortoise-> runAction (repeatAction );
Parallel Spawn action
A batch of actions are executed simultaneously. The executed action must be an action that can be simultaneously executed and inherited from FiniteTimeAction. The final completion time of the combined Spawn is determined by the maximum execution time of its members.
Sequence action
Execute a series of actions in sequence. Some non-delayed actions are not supported, such as RepeatForever.
DelayTime delayed action
It indicates a blank period in the action sequence. Different actions are concatenated by placeholder, and nothing is done.
4. Variable Speed
Speed is used to linearly change the Speed of an action, so that the action continues for a longer or shorter time. A Speed action cannot be part of an action sequence because it is not an ActionInterval object.
// Change the speed of the MoveTo action. auto moveToAction = MoveTo: create (10.0f, Vec2 (100,100); // the two parameters are respectively the target action and the speed change ratio, if the variable Speed ratio is set to 1, the Speed of the target action will not be changed. auto speedAction = Speed: create (moveToAction, 1); // you can set the tag attribute of the action, similar to the tag attribute of Node, it is used to conveniently find the action speedAction-> setTag (0); sprite-> runAction (speedAction); // you can set a timer, 4 seconds later, the MoveTo speed will be changed to 10 times the original speed. this-> scheduleOnce (schedule_selector (HelloWorld: changeSpriteSpeed), 4); // The timer implements void HelloWorld :: changeSpriteSpeed (float data) {Speed * speed = dynamic_cast
(Sprite-> getActionByTag (0); speed-> setSpeed (10 );}
ActionEase
Speed can only change the speed of the target action proportionally. If we want to change the Speed of the action from fast to slow, and the speed changes over time, we need to constantly modify its Speed attribute to achieve it. ActionEase solves this problem by using multiple built-in automatic speed changes.
ActionEase actions can be summarized into five categories: exponential buffering, Sine buffering, elastic buffering, jump buffering, and response buffering. Each type of action has three changes In different periods: In, Out, And InOut.
auto moveToAction = MoveTo::create(10.0f, Vec2(100, 100));auto easeSineInAction = EaseSineIn::create(moveToAction);tortoise->runAction(easeSineInAction);