cocos2d-Action (Action) detailed

Source: Internet
Author: User
Basic Principles

An action class is the base class for all actions, and the object that is created represents an action. Actions work on node, so each action needs to be performed by the node object. The action class, as the base class, is actually an interface, and most of its implementation classes are derived from the finite-time action Class (Finitetimeaction), which is inherited from the actual development of both immediate and continuous actions. Instant Action

Instant actions are actions that can only be done immediately, such as actions that are done immediately at the next frame, such as setting the position and setting the zoom. When they are packed into action, they can be combined with other action classes for complex movements. Common Instant Action-place

This action places the node at a specified location, which acts on the same position property as the Modify node, such as placing the node at screen coordinates (10,10):

Auto Placeaction = place::create (Point (10, 10));
common Instant Action-flipx and flipy

The purpose of these two actions is to reverse the sprite along the x and Y axes, the same as setting the FlipX and FlipY properties of the Sprite, wrapping it as an action for easy combination with other actions. For example:

Auto Flipxaction = Flipx::create (true);
Auto MoveTo = moveto::create (0.4f, point (0, 0));
Auto action = Sequence::create (MoveTo, Flipxaction, Moveto->reverse (), NULL);

Sequence is an action sequence, reverse is the inverse of the original action, so this piece of code is to move the sprite to one end after the reverse display and then back to the original point. common instant Action-show and hide

These two actions are used to show and hide nodes, as they do with the visible property of the set node. For example, in order for the wizard to hide after the move is complete, you can use the following code:

Auto hideaction = Hide::create ();
    Auto MoveTo = moveto::create (0.4f, point (0, 0));
    Auto action = Sequence::create (MoveTo, hideaction, NULL);
Common Instant Action-callfunc

Callfunc actions include the Callfunc and CALLFUNCN two actions, which are used to make method calls in the action. In order to conserve memory resources in the design, you can call the appropriate function to clean up the memory after the action is completed, for example:

Auto Actionmovedone = Callfuncn::create ([&] (ref* sender) {
        log ("Clear memory");
    });
    Auto MoveTo = moveto::create (0.4f, point (0, 0));
    Auto action = Sequence::create (MoveTo, Actionmovedone, NULL);
Continuous Action Property Change Action

The property change action is animated by the gradual change of the property value. It is important to note that the difference between Xxto and Xxby is that Xxto is the final value and Xxby is the vector-changing value. attribute change action-moveto and Moveby

For the node to do linear motion, set the action time and the end point position, in the specified time will be moved to the end point, their initialization method is as follows:

Moveto::create (float duration, const point& position);
    Moveby::create (float duration, const point& position);

The position value of MoveTo represents the last position, while the position of Moveby represents the location of the move. attribute change action-jumpto and Jumpby

The nodes jump to the specified position with a certain trajectory, and they are initialized as follows:

Jumpto::create (float duration, const point& position, float height, int jumps);
    Jumpby::create (float duration, const point& position, float height, int jumps);
attribute change action-bezierto and Bezierby

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 point determines the shape of the curve, including the angle and length of two parameters. The following figure:

When using, we must first create the CCBEZIERCONFIG structure, set the end Endposition and two control points controlpoint_1 and Controlpoint_2, Then the struct is passed into the initialization method of Bezierto or Bezierby:

Ccbezierconfig Bézier;
    bezier.controlpoint_1 = Point (0, 0);
    Bezier.controlpoint_2 = point (at +);
    Bezier.endposition = Point (in.);
    Auto Bezieraction = Bezierto::create (0.5f, Bézier);
attribute change action-scaleto and Scaleby

Produces a scaling effect that causes the node's zoom factor to change linearly with time, corresponding to the initialization method:

Scaleto::create (float duration, float s);
    Scaleby::create (float duration, float s);
attribute change action-rotateto and Rotateby

Produces a rotation effect, corresponding to the initialization method:

Rotateto::create (float duration, float deltaangle);
    Rotateby::create (float duration, float deltaangle);
visual effects action-fadein, fadeout and Fateto

Produces a fade effect, and a transparent change effect, corresponding to the initialization method:

Fadein::create (float d);    Fade
    fadeout::create (float d);    Fade 
    Out fadeto::create (float duration, glubyte opacity); Change in transparency over time
visual effects action-tintto and Tintby

Set the hue change, this action is less used, the initialization method is:

Tintto::create (float duration, glubyte red, Glubyte Green, Glubyte blue);
    Tintby::create (float duration, glubyte red, Glubyte Green, Glubyte blue);
visual effects Action-blink

The node flashes, and the initialization method is:

Blink::create (float duration, int blinks);

Blinks is the number of flashes. visual effects Action-animation

Animate the animation in the form of frames, the following two ways to achieve the sprite frame animation effect:

Manually create the animation
    Auto animation = Animation::create ();
    for (int i=1;i<15;i++)
    {
        char szname[100] = {0};
        sprintf (SzName, "sprite_%02d.png", I);
        Animation->addspriteframewithfile (SzName);
    }

    Animation->setdelayperunit (2.8f/14.0f);
    Animation->setrestoreoriginalframe (true);

    Auto action = animate::create (animation);
    Sprite->runaction (Sequence::create (Action, Action->reverse (), NULL));


    File creation Animation
    Auto cache = Animationcache::getinstance ();
    Cache->addanimationswithfile ("Animation.plist");
    Auto Animation2 = cache->getanimation ("Dance_1");

    Auto Action2 = animate::create (animation2);
    Sprite->runaction (Sequence::create (Action2, Action2->reverse (), NULL));

After the animation is created, an animation player is required to play these animations, which is animate. Compound Action

Usually in development we need to combine various actions to make the nodes execute, the action of compound action is to combine various actions together. Moreover, the compound action itself is an action. So it can be embedded in other actions as a normal action.
Note: Sequence action cannot be embedded in other compound actions, delaytime is not a compound action, but can only be used within a compound action. Compound Action-delaytime

The delay action actually does nothing, provides a blank period, it has only one initialization method:

Delaytime::create (float D);
Compound Action-repeat/repeatforever
Repeat::create (finitetimeaction *action, unsigned int times);
    Repeatforever::create (Actioninterval *action);
Compound Action-spawn

To make a batch of actions execute simultaneously, the two initialization methods are:

Spawn::create (Finitetimeaction *action1, ...);
    Spawn::create (const vector<finitetimeaction*>& arrayofactions);
Compound Action-sequence

To allow various actions to be executed sequentially, the two initialization methods are:

Sequence::create (Finitetimeaction *action1, ...);
    Sequence::create (const vector<finitetimeaction*>& arrayofactions);
Variable speed movement

The variable speed movement is similar to the compound action and is a special action that can be executed at the speed of any change. variable speed movement-speed

Used to linearly change the speed of an action, in order to change the speed of an action, the first thing to do is to wrap the target action into the tempo action:

Auto repeat = repeatforever::create (animation);
    Auto Speed = Speed::create (repeat, 0.5f);
    Sprite->runaction (speed);

The second parameter is the variable speed ratio, which is set to 0.5f at the same speed as half. variable speed movement-actionease

Although speed can change the velocity of the action, but only proportionally change the speed, actionease can achieve the speed of the movement from fast to slow, speed changes with the uniform motion. This class contains 5 types of motion, exponential buffering, sine buffering, elastic buffering, jump buffering, and back-shock buffering. Each type of movement consists of 3 different periods of transformation: in, out, and inout.
Take Insine as an example:

Auto Sinein = easesinein::create (action);
    Sprite->runaction (Sinein);

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.