Cocos2d-x 3.x Action

Source: Internet
Author: User

Action is the base class for actions, and all actions derive from this class, and an object it creates represents an action. Actions work on node, so any action needs to be performed by a node object.

Move an elf from the middle of the screen for 3 seconds to the position of (0, 0) 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 preserves some of the intermediate parameters that continue to change during the action.

Action as a base class is essentially an interface (abstract class), and the implementation classes derived from it (such as motion and rotation) are the actions we actually use. The majority of the implementation classes of action derive from the subclass finitetimeaction of the action, which defines the actions that can be done within a limited time. Finitetimeaction defines the reverse method, which can be used to obtain an action opposite to the original action (inverse action), such as hiding an elf, and then displaying it with the inverse action. Not all actions should be reversed, such as "Zoom to" and so on, such as the setting of the property is a constant action does not have the inverse action, and the Set property is relative value of the action often there is a corresponding inverse action.

The two main classes derived from Finitetimeaction are instantaneous action actioninstant and persistent action Actioninterval.
Instantaneous action refers to an action that can be completed immediately, which is a special case in which the duration of action in Finitetimeaction is 0. This type of action is performed immediately at the next frame, such as setting the position, setting the zoom, and so on. This type of action can be done simply by assigning a value to node, but wrapping them as an action can easily be combined with other action classes as complex actions.

1. Some commonly used instantaneous actions:

Place

This action is used to place the node at a specified location, which acts on the same position property as the modified node.
Place the sprite at the screen coordinates (200, 200) Auto placeaction = Place::create (VEC2); tortoise->runaction (placeaction);

FlipX and FlipY

These two actions are used to reverse the sprite along the x and Y axes, with the same effect as setting the FlipX and FlipY properties of the sprite.
Reverses 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, with the same effect as the set node's Visible property.
Hidden Sprite Auto hideaction = Hide::create (); tortoise->runaction (hideaction);

Callfunc

Callfunc series actions include __CCCALLFUNCND,__CCCALLFUNCO,CALLFUNCN, which are used to perform function calls in the action. When an object executes a Callfunc series of actions, it invokes a pre-set method to accomplish some particular function.
The suffix "N" of the Callfunc series action means the node parameter, which refers to the object that executes the action, "D" means the data parameter, refers to the user-defined information, "O" represents the object and refers to a user-defined object (REF) parameter.
A function call in an action can be a class member function, or it can be a normal function//void Fun (node *node) normal function void Helloworld::fun (node *node) {Cclog ("CALLFUNCN");} Auto Callfuncaction = callfuncn::create (fun); Call normal function in action auto Callfuncaction = Callfuncn::create (This, Callfuncn_selector (Helloworld::fun)); Call the class member function tortoise->runaction (callfuncaction) in the action;

2. Continuous action is the action that is performed over a period of time, and most of the persistent actions carry a real parameter duration that controls the execution time of the action. Each continuous action usually has two different variant actions, with the to and by suffixes: the action suffix to is describes the absolute change of the node attribute value, for example MoveTo moves the object to a specific position, and the action of the prefix by is a description of the relative change of the property value. such as Moveby moves the object a relative displacement.
Depending on the effect, the continuous action can be divided into 4 categories: Position change action, property change action, visual effect action, control action.

Position change action

For the location (position) property, the engine provides us with 3 types of positional change actions.

MoveTo and Moveby are used for linear motion of the nodes.

Within the specified time, move straight from the current position to the set end position Auto Movetoaction = moveto::create (3.0f, Vec2 (0, 0)); Tortoise->runaction (movetoaction) ;//In the specified time, relative to the current position of the move (100, 100), the effect is to the upper right corner of the mobile auto movebyaction = Moveby::create (3.0f, VEC2);tortoise-> Runaction (movebyaction);

Jumpto and JUMPBY nodes jump to the specified position with a certain trajectory

Within the specified time, jumping from the current position to the set end position, the first parameter represents the duration, the second parameter represents the end position of the move, the third represents the maximum jump height, and the fourth parameter indicates the number of hops auto jumptoaction = jumpto::create ( 3.0f, Vec2 (0, 0), 50.0f, 5); tortoise->runaction (jumptoaction);//In the specified time, relative to the current position jump (300, 300), the effect is to jump to the upper right corner auto Jumpbyaction = Jumpby::create (3.0f, VEC2 (+), 50.0f, 5); tortoise->runaction (jumpbyaction);

Bezierto and Bezierby nodes are curved, and the motion trajectory is described by Bezier curves. Each Bezier curve contains a starting point and an end point. In a curve, the start and end points each contain a control point, and the connection point to the endpoint is called the control line. The control line determines the shape of the curve emitted from the endpoint, including the angle and length of two parameters, the angle determines the direction of the curve it controls, that is, the curve in the tangent direction of this control point, length control curve curvature. The longer the control line, the more closely the curve it controls is closer to the control line. Any curve can consist of a Bezier curve connected to a segment or segments.

You need to create the CCBEZIERCONFIG structure first, set the end Endposition and two control points controlpoint_1 and Controlpoint_2, Then the structure is passed into the Bezierto or Bezierby initialization method Ccbezierconfig bezier;bezier.controlpoint_1 = VEC2 (); bezier.controlpoint_2 = VEC2 (bezier.endposition = Vec2); auto beziertoaction = Bezierto::create (3.0f, Bézier);tortoise-> Runaction (beziertoaction);

Property Change Action

Animate effects through gradual changes in property values.

Scaleto and Scaleby produce scaling effects

The second parameter is a scaling factor auto scaletoaction = Scaleto::create (3.0f, 2); tortoise->runaction (scaletoaction);

Rotateto and Rotateby produce rotational effects

The second parameter is an angle, the positive direction is clockwise auto Rotatetoaction = Rotateto::create (3.0f, 30.0f); Tortoise->runaction (rotatetoaction);

Fadein and fadeout fade effect, the latter achieves fade out effect

Fadeto setting the effect of transparency over time

Tintto and Tintby setting tonal variations

Only nodes that implement the Ccrgbaprotocol interface can perform such actions because properties related to transparency or color inherit from the Ccrgbaprotocol interface. Many common nodes, such as sprites and Layercolor, implement this interface.
Create an action with duration and opacity, the second parameter, the Glubyte type, is a 8-bit unsigned integer, so any integer in 0-255 is preferable, and transparency-related actions can only be applied to the sprite, and the child nodes are not affected by the parent node. Auto Fadetoaction = fadeto::create (3.0f), tortoise->runaction (fadetoaction);//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 action

Blink to flash the target node

Blinks 5 times in 3 seconds auto blinkaction = Blink::create (3.0f, 5); tortoise->runaction (blinkaction);

Animate Play Frame animation

Control action

The control action is used for fine-grained control of the action.

Delaytime the action for a period of time.

Repeat repeat the existing action a certain number of times.

RepeatForever keeps one movement repeating.


3. Compound action

Combine various basic actions to produce complex action effects.

Repeat and RepeatForever

Repeat action Auto repeataction = Repeat::create (blinkaction, 3); tortoise->runaction (repeataction);

Spawn Parallel Action

A batch of actions executes simultaneously. The action performed must be an action that can be performed concurrently, inheriting from Finitetimeaction. When combined, the spawn final finish is determined by the action of the maximum execution time of its members.

Sequence sequence action

Perform a series of actions sequentially. Some non-delay actions are not supported, such as RepeatForever.

Delaytime Delay Action

Represents a blank period in an action sequence, and the different actions are threaded together in a placeholder way, doing nothing.

4. Variable speed movement

Speed is used to linearly change the velocity of an action so that the action lasts longer or shorter. The speed action cannot be part of an action sequence because it is not a Actioninterval object.

Change the speed of the MoveTo action auto movetoaction = Moveto::create (10.0f, VEC2 (100, 100));//two parameters are target action and variable speed ratio, set the variable speed ratio of 1, The speed of the target action will not be changed auto Speedaction = Speed::create (movetoaction, 1);//Set the Tag property of the action, similar to the Tag property of node, for easy find action speedaction-> Settag (0); sprite->runaction (speedaction);//Set a timer, 4 seconds after the MoveTo action speed into the original 10 times Times this->scheduleonce (Schedule_ Selector (helloworld::changespritespeed), 4);//Timer implementation void Helloworld::changespritespeed (float data) {Speed *speed = Dynamic_cast<speed *> (Sprite->getactionbytag (0)); speed->setspeed (10);}

Actionease

Speed can only be scaled to change the velocity of the target action, if we want to achieve the action from fast to slow, speed over time changes in the variable motion, need to constantly modify its tempo property to achieve. The Actionease series, however, solves this problem by using a variety of built-in automatic speed changes.
Actionease actions can be generalized as 5 types of actions: exponential buffer, sine buffer, elastic buffer, jump buffer, and back shock buffer. Each type of action has 3 different periods of change: in, out, and inout.
Auto Movetoaction = Moveto::create (10.0f, VEC2 (+)), auto easesineinaction = Easesinein::create (movetoaction); Tortoise->runaction (easesineinaction);

Cocos2d-x 3.x Action

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.