Rookie study cocos2d-x 3.x--on action action

Source: Internet
Author: User
Tags time interval
Action class overview

A game, design again NB game, if all is a bunch of static pictures, no action, that can only "hehe". The action system has a very important influence on the success of a game. So, this article summarizes the actions in Cocos2d-x. Let's take a look at the action-related classes in Cocos2d-x.

The class diagram associated with the action is shown in the following diagram: These classes are now briefly described, and detailed analysis will be done in subsequent sections. Ref and clonable: Do not say here, in summing up the cocos2d-x memory management when the detailed summary; action: The parent of all actions, defines the common operation, Finitetimeaction: The parent class of the instantaneous action and the delay action, can define the time change of the action ; follow: Follow the action of the node; speed: Change the time of an action, such as slow-motion playback or fast-forward, Actioninstant: Instantaneous completion of the action, no animation effect in the middle; Actioninterval: The action will be done within the specified time, The middle will have animation effect, flipx:x axis direction Flip, MoveTo: Move action;

The following is a summary of the actual code for the classes described above. the main member function of the action class

The following are the main member functions of the action class:

/**
* Returns a new Action object that represents the opposite action of the original action
*
/virtual action* reverse () const = 0;

Returns true if the action is complete,
virtual bool IsDone () const;

Called before the action is started, the object that sets the action is
virtual void Startwithtarget (Node *target);

/**
* will be called after the action is completed, it will set the "target" object to be empty
* Note: Please never call this function manually, but instead call the corresponding "target->stopaction (action);"
*
/virtual void stop ();

/**
* Every frame will call the method, if you need to control the action at each frame, you need to rewrite, the time interval is the action interval
* Best not to rewrite the function, unless you really know how to do *
*
virtual void Step ( float DT);

/**
* Each frame is called once, the parameter time value is any value between 0 and 1, for example:
* 0 means that the action is just the beginning of the call; * 0.5 is called when the action is
executed halfway;
* 1 indicates that the action is completed after the call ;
*
/virtual void update (float time);

Gets the object that executes the action
inline node* gettarget () const {return _target;}

Sets the object that performs the action
inline void settarget (Node *target) {_target = target;}

Action label
Inline int Gettag () const {return _tag;}
inline void Settag (int tag) {_tag = tag;}
Instantaneous Action

Actioninstant is an instantaneous action class. Instantaneous action means instantaneous completion of the action, the middle without any animation effect; Because of the actioninstant of the sub-class, here flipx as an example, do a simple demo. The effect is as follows: You can see the puppy walking to the far left, there will be a steering process, which is the flipx action effect. The sample code is as follows:

Size visiblesize = director::getinstance ()->getvisiblesize ();

Create move action
actioninterval *moveto = moveto::create (5, VEC2 (0, +));

Creates a rollover action in the x-axis direction
actioninstant *flipx = Flipx::create (true);
Actioninterval *moveback = moveto::create (5, VEC2 (Visiblesize.width,));

Auto scene = Director::getinstance ()->getrunningscene ();
Auto Layer = Scene->getchildbytag (1);
Auto Dog = Layer->getchildbytag (1);

Flipx->reverse () get the corresponding reverse action
auto action = sequence::create (MoveTo, FlipX, Moveback, Flipx->reverse (), NULL) ;
Dog->runaction (action);

An instantaneous action is an action that can be done immediately, such as an action that is done immediately at the next frame, such as setting a position, setting a scale, and so on. After wrapping them in motion, they can be combined with other action classes for complex movements. Delay Action

The action will be completed within the specified time and will have an animated effect in the middle. The delay action is animated by the gradual change of the attribute 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. such as the MoveTo and moveby actions, as shown in the following: The sample code is as follows:

Create move action
actioninterval *moveto = moveto::create (5, VEC2 (0, +));
Actioninterval *moveby = moveby::create (5, VEC2 ( -200, 0));

Auto scene = Director::getinstance ()->getrunningscene ();
Auto Layer = Scene->getchildbytag (1);
Auto Dog1 = Layer->getchildbytag (1);
Auto DOG2 = Layer->getchildbytag (2);

Dog1->runaction (moveto);
Dog2->runaction (Moveby);

The dog above is a moveby action, and the dog below is using the MoveTo action. MoveTo actions are all in use, here's a look at the slightly more complex Bézier curve action.

With Bezier curves, you can make the nodes move in a curved motion. 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 illustration: The following: The sample code is as follows:

Ccbezierconfig Bézier;
Bezier.controlpoint_1 = point (at +);
Bezier.controlpoint_2 = point (at +);
Bezier.endposition = Point (a);
Auto Bezieraction = Bezierto::create (2.0f, Bézier);

Dog1->runaction (bezieraction);

In our actual development, the main task is to identify two control points to coordinate the sprite's moving radians. Buffering Action

In the game, we often want to achieve some acceleration or reduce the effect of speed. Cocos2d-x has done for us.

The Actionease class can realize the uniform motion of the action speed from fast to slow, and the speed changes with time. This class contains 5 types of motion: exponential buffer, sine buffer, elastic buffer, jumping buffer, and back cushion.

Each type of movement consists of 3 different periods of transformation: in, out, and inout. In means accelerating at the beginning; out means accelerating at the end; inout means accelerating at the beginning and end.

The above 5 types of motions correspond to the following classes: Exponential buffers: Easeexponentialin, easeexponentialout, and easeexponentialinout; sine buffering: Easesinein, Easesineout and easesineinout; elastic buffers: Easeelasticin, easeelasticout and easeelasticinout; jumping buffers: Easebouncein, Easebounceout and Easebounceinout; back cushion: Easebackin, Easebackout and Easebackinout.

Through the chart and description can not figure out the above 5 movement, the following through the actual performance of the index buffer to show:

The index buffer sample code is as follows:

actioninterval *moveto1 = moveto::create (5, VEC2 (50, 100));
Actioninterval *moveto2 = moveto::create (5, VEC2 (50, 300));

Actioninterval *moveto3 = moveto::create (5, VEC2 (50, 500));
Auto scene = Director::getinstance ()->getrunningscene ();
Auto Layer = Scene->getchildbytag (1);
Auto Dog1 = Layer->getchildbytag (1);
Auto DOG2 = Layer->getchildbytag (2);

Auto Dog3 = Layer->getchildbytag (3);
Auto Action1 = easeexponentialin::create (moveto1);
Auto Action2 = easeexponentialout::create (Moveto2);

Auto Action3 = easeexponentialinout::create (Moveto3);
Dog1->runaction (Action1);
Dog2->runaction (Action2);
Dog3->runaction (Action3); 

From bottom to top three puppies, respectively, correspond to Easeexponentialin, Easeexponentialout and Easeexponentialinout. The effect of Easeexponentialin performance is faster and faster, the effect of easeexponentialout performance is more and more slow, the effect of easeexponentialinout performance is very fast in the middle speed, the two ends are slower. Combo Action

In the game, a lot of times, an object is not simply to perform an action, but to perform a series of actions in turn or to perform a series of actions simultaneously, then how to do it. Now let's see how this is done in cocos2d-x. Sequence can use Sequence to define an action sequence, and the application instance can refer to the Instantaneous Action section. Spawn Spawn also defines a series of actions, but the defined actions are executed simultaneously, using the same method as the sequence

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.