Cocos2d-x 3.0 Action, cocos2d-xaction

Source: Internet
Author: User

Cocos2d-x 3.0 Action, cocos2d-xaction

* *********************************** Please specify the source: bytes ********************************************




Digress:

Long time, long time, wood has been updated

In February August, I traveled to South Korea for nine days. When I came back, I went directly to Jinggangshan to participate in practical activities (the subject is to build an Android Application)

Well, now we are working at the Jinggangshan practice base. We can only go back on the 19th.

In addition, I finally connected to WIFI and can post a blog.



Body:

This is about cocos2d-x Action, is Action, all kinds of Action, this learning, it is best according to the sample of cocos2d-x.

{

PS: how to open the sample program:

Open the project with VS2012:

-> Under the cocos2d-x folder

-- Build folder

Cocos2d-win32.vc2012 Project

}



This blog post demonstrates some basic actions through seven test programs:

1. Manual Transformation-Basic settings for the genie

Ii. MoveTo and MoveBy-basic actions of the genie (sequential Actions)

3. Span + Rotate-basic actions of the genie (execute actions at the same time)

4. DelayTime: m + delay + m ---- delayed action execution

V. Repeat and RepeatForever-repeated execution

6. Follow Action ---- Follow Action

VII. Sequence of InstantActions ---- termination mechanism



Well, you can run the program while watching the code and learning.

For the file, see ActionTest. cpp.

In the running program, select Action-Basic to view






-- First test --

Manual Transformation


Its corresponding code is located:

//------------------------------------------------------------------//// ActionManual////------------------------------------------------------------------void ActionManual::onEnter(){    ActionsDemo::onEnter();    auto s = Director::getInstance()->getWinSize();    _tamara->setScaleX( 2.5f);    _tamara->setScaleY( -1.0f);    _tamara->setPosition( Point(100,70) );    _tamara->setOpacity( 128);    _grossini->setRotation( 120);    _grossini->setPosition( Point(s.width/2, s.height/2));    _grossini->setColor( Color3B( 255,0,0));    _kathia->setPosition( Point(s.width-100, s.height/2));    _kathia->setColor( Color3B::BLUE);}std::string ActionManual::subtitle() const{    return "Manual Transformation";}


You can see from the code,

A series of operations on objects:

// Actions /// ActionManual /// -------------------------------------------------------------------------- void ActionManual: onEnter () {ActionsDemo: onEnter (); // obtain the size of the OpenGL view in points auto s = Director: getInstance ()-> getWinSize (); // perform operations on the first object: // scale the X axis _ tamara-> setScaleX (2.5f); // scale the Y axis _ tamara-> setScaleY (-1.0f ); // set the coordinates of _ tamara-> setPosition (Point (128); // set the transparency of _ tamara-> setOpacity (); // The operations performed on the second object: // set the rotation angle _ grossini-> setRotation (120); _ grossini-> setPosition (Point (s. width/2, s. height/2); // set the color of _ grossini-> setColor (Color3B (255, 0); <span style = "white-space: pre "> </span> // operation on the third object: _ kathia-> setPosition (Point (s. width-100, s. height/2); _ kathia-> setColor (Color3B: BLUE);} std: string ActionManual: subtitle () const {return "Manual Transformation ";}


Maybe you have noticed that the function here is OnEnter. Why is it not Init?

Here we will talk about the differences between OnEnter and Init:

Init: The function executed during object initialization.

OnEnter: the function to be executed when the object is displayed. (Executed only when visible)




-- Second test --

The second test is about MoveTo and MoveBy.

Code:

// Actions /// ActionMove /// define void ActionMove: onEnter () {ActionsDemo: onEnter (); centerSprites (3); auto s = Director :: getInstance ()-> getWinSize (); // set several actions: // move to an absolute coordinate point. The first parameter is the elapsed time auto actionTo = MoveTo :: create (2, Point (s. width-40, s. height-40); // move to a relative coordinate point, that is, to 80 to the right based on the current position, and to 80. The first parameter is the time spent auto actionBy = MoveBy :: create (2, Point (80, 80); // performs an actionBy reverse action. auto actionByBack = actionBy-> reverse (); // arrange the actions executed separately for the three genie. // The first genie executes the actionTo Action _ tamara-> runAction (actionTo ); // The second one performs the actionBy and actionByBack actions sequentially (actionBy is executed first and then actionByBack) _ grossini-> runAction (Sequence: create (actionBy, actionByBack, NULL )); // The third genie executes the MoveTo action (content: In 1 second, up, and 40 to the right) _ kathia-> runAction (MoveTo: create (1, point (40, 40);} std: string ActionMove: subtitle () const {return "MoveTo/MoveBy ";}

Sequence can be used to execute a series of actions in Sequence.


You can view the following information on your own,

With the help of APIS, it is easy to understand.





-- Third test program --

This test program is Span + Rotate.

//------------------------------------------------------------------//// ActionSpawn////------------------------------------------------------------------void ActionSpawn::onEnter(){    ActionsDemo::onEnter();    alignSpritesLeft(1);    auto action = Spawn::create(        JumpBy::create(2, Point(300,0), 50, 4),        RotateBy::create( 2,  720),        NULL);    _grossini->runAction(action);}std::string ActionSpawn::subtitle() const{    return "Spawn: Jump + Rotate";}

This means that two actions are executed simultaneously.

JumpBy: create (2, Point (, 0), 50, 4)

Skip statement.

RotateBy: create (2,720)

Flip statement.

Similar to Sequence, Spawn can be used to perform multiple actions simultaneously.




-- Fourth test program --

This is DelayTime: m + delay + m

That is, add a delay time in the middle:

//------------------------------------------------------------------//// ActionDelayTime////------------------------------------------------------------------void ActionDelayTime::onEnter(){    ActionsDemo::onEnter();    alignSpritesLeft(1);    auto move = MoveBy::create(1, Point(150,0));    auto action = Sequence::create( move, DelayTime::create(2), move, NULL);    _grossini->runAction(action);}std::string ActionDelayTime::subtitle() const{    return "DelayTime: m + delay + m";}

Obviously, Sequence is used for sequential execution, and a delayed statement DelayTime is added in the middle.




-- Fifth test program --

Repeat and RepeatForever

//------------------------------------------------------------------//// ActionRepeat////------------------------------------------------------------------void ActionRepeat::onEnter(){    ActionsDemo::onEnter();    alignSpritesLeft(2);    auto a1 = MoveBy::create(1, Point(150,0));    auto action1 = Repeat::create(        Sequence::create( Place::create(Point(60,60)), a1, NULL) ,         3);     auto  action2 = RepeatForever::create(        Sequence::create(a1->clone(), a1->reverse(), NULL)        );    _kathia->runAction(action1);    _tamara->runAction(action2);}std::string ActionRepeat::subtitle() const{    return "Repeat / RepeatForever actions";}

Repeat executes a certain number of actions.

Repeat: create has two parameters: the first is the action that needs to be repeated, and the second is the number of repetitions.

The number of repetitions ranges from 1 to 2 ^ 30.

The corresponding RepeatForever has no number of parameters, but only one parameter.




-- Sixth test program --

Follow Action

// Outputs //// ActionFollow /// ------------------------------------------------------------------ void ActionFollow: onEnter () {ActionsDemo: onEnter (); centerSprites (1); auto s = Director :: getInstance ()-> getWinSize (); // set the coordinate position for the genie _ grossini-> setPosition (Point (-200, s. height/2); // set the action auto move = MoveBy: create (2, Point (s. width * 3, 0); auto move_back = move-> reverse (); // set the Sequence of actions: auto seq = Sequence: create (move, move_back, NULL ); // set the action to always repeat auto rep = RepeatForever: create (seq); _ grossini-> runAction (rep); // set the following statement. creat has two parameters, the first is the genie to be followed, and the second parameter is the boundary of the action this-> runAction (Follow: create (_ grossini, Rect (0, 0, s. width * 2-100, s. height )));}

In the following statement, the second parameter is about the action boundary. When the parameter is set to Rect: ZERO, there is no boundary.




-- Seventh test program --

Sequence of InstantActions

// Activities //// ActionSequence2 //// required void ActionSequence2: onEnter () {ActionsDemo: onEnter (); alignSpritesLeft (1); // you can specify whether the node is visible, false: invisible _ grossini-> setVisible (false); // sets the action auto action = Sequence: create (Place: create (Point (200,200 )), show: create (), MoveBy: create (1, Point (, 0), // when the preceding three actions are completed, call the following three CallFunc :: create (CC_CALLBACK_0 (ActionSequence2: callback1, this), CallFunc: create (CC_CALLBACK_0 (ActionSequence2: callback2, this, _ grossini), CallFunc :: create (CC_CALLBACK_0 (ActionSequence2: callback3, this, _ grossini, 0 xbebabeba), NULL); _ grossini-> runAction (action );}

CallFunc: In the create statement,

CC_CALLBACK_0 indicates no parameter, which is applied when Action is used.

CC_CALLBACK_1 indicates that there is only one parameter. It is applied when the Menu is used.

CC_CALLBACK_2 indicates that two parameters are bound, which are generally used for single-point touch and multi-point touch.



* ***** To sum up ******


Action:

-- Basic action

MoveTo/MoveBy

RoatTo/RoatBy

JumpTo/JumpBy

.......

-- Combined action

Sequential Sequence execution

Run Spawn simultaneously

-- Reverse of an action

Reverse

-- Repeated action execution

Repeat

RepeatForever

-- Function action

CallFucn: create ()


Well, it's here ~




References: Instructor Shen


* *********************************** Please specify the source: bytes ********************************************


Cocos2d-x Sprite Action problems

# Define THETAG 123/* set this number as needed */
Initialization action:
CCAction * pAction = the action you want to execute;
PAction-> setTag (THETAG );
PSprite-> runAction (pAction); // the action you performed
Acceleration:
PSprite-> stopActionByTag (THETAG );
PAction = CCSpeed: actionWithAction (
The action you want to perform/* Note that the time must be set to 20 seconds, because the acceleration actually takes 10 seconds to execute 20 seconds */,
2/* if you want to slow down, it will be 0.7 */);
PAction-> setTag (THETAG );
PSprite-> runAction (CCSequence: actions (
PAction,
CCCallFunc: actionWithTarget (pSprite, callfunc_selector (callback function )),
NULL

));

The following is the internal callback function: // This function is a member function of the genie that executes the action.
StopActionByTag (THETAG );
CCAction * pAction = the action you want to execute;
PAction-> setTag (THETAG );
RunAction (pAction );

Cocos2d-x "actionWithDuration": Not a member of "cocos2d: CCMoveTo ??

In 2.0, many functions are renamed create.

Related Article

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.