COCOS2DX several elves play the animation solution in sequence

Source: Internet
Author: User
Tags addchild

Let me describe the problem first:

Take the card game before, if there are 3 cards on the field, then you must play the attack animation in a sequence, I am from left to right way.


My solution is to pass a delay parameter to each card, then use Delytime when runaction, but this method is too troublesome!


Now let's talk about a better way:

The basic idea is the combination of vector and callfunc.


One:

Define data and functions in a class first

std::vector<std::vector<int>> v_action;//Storage Wizard tag and animation tag container void push_action (int sprite_tag,int action_ tag);//Add Action sequence void run_action (int sprite_tag,int action_tag);//Play Action void next_action ();//Next action sequence* run (int action _tag);//Return action according to tag BOOL isrunaction;//whether the action is being played

Two:

Initialize parameters in Init

There is no play action isrunaction=false;    Size visiblesize = director::getinstance ()->getvisiblesize (); VEC2 origin = Director::getinstance ()->getvisibleorigin ();//actor One auto Actor1 = Sprite::create ("Closenormal.png"); Actor1->settag (one); Actor1->setposition (250,VISIBLESIZE.HEIGHT/2); This->addchild (actor1,2);//actor Two auto Actor2 = Sprite::create ("Closenormal.png"); Actor2->settag (actor2->setposition) 300,VISIBLESIZE.HEIGHT/2                                           ); This->addchild (actor2,2);//actor jumps Auto Play1 = menuitemimage::create ("Closenormal.png", "Closeselected.png", Cc_callback_1 (Helloworld::menucallback, this));p Lay1->settag (1);p lay1->setposition (250,VISIBLESIZE.HEIGHT/5);//actor two jumps auto Play2 = Menuitemimage::create ("                                           Closenormal.png "," Closeselected.png ", Cc_callback_1 (Helloworld::menucallback, this));p Lay2->settag (2);p lay2->setpositIon (300,VISIBLESIZE.HEIGHT/5);                                           Auto Closeitem = menuitemimage::create ("Closenormal.png", "Closeselected.png", Cc_callback_1 (helloworld::menuclosecal    Lback, this));                                Closeitem->setposition (VEC2 (origin.x + visiblesize.width-closeitem->getcontentsize (). WIDTH/2,    ORIGIN.Y + closeitem->getcontentsize (). HEIGHT/2));    Create menu, it's an Autorelease object auto menu = Menu::create (Play1,play2,closeitem, NULL);    Menu->setposition (Vec2::zero); This->addchild (menu, 1);


The above created two sprites and two buttons (another ignore), click Play1 then actor 1 jump, click Play2 Actor 2 Jump. In order to realize that two actors do not take off at the same time and record the order of our operations, we need to write this in Menucallback:
void Helloworld::menucallback (ref* psender) {Auto MenuItem = (menuitemimage*) Psender; sprite* Actor;switch (Menuitem->gettag ()) {case 1:push_action (11,1); Break;case 2:push_action (22,1); Break;default : Break;}}

The Push_action function is used here to play the action or to deposit the operation into the container:

void HelloWorld::p ush_action (int sprite_tag,int action_tag) {if (Isrunaction==false)//If there is no action in play, play this action directly { Isrunaction=true;run_action (Sprite_tag,action_tag);} else//if it is playing, then put this action into the container {std::vector<int> v_sprite;v_sprite.push_back (Sprite_tag); V_sprite.push_back ( Action_tag); V_action.push_back (V_sprite);}}

The note has been more detailed ~

Let's see what's in the Run_action function first:

void helloworld::run_action (int sprite_tag,int action_tag) {Auto actor = (sprite*) getchildbytag (sprite_tag); auto Squence = Sequence::create (Run (Action_tag), Callfunc::create (This,callfunc_selector (helloworld::next_action)), NULL); actor->runaction (squence);}

The function is to play the action according to the incoming two tag, and then call Next_action:
void Helloworld::next_action () {if (V_action.empty ()) {isrunaction=false;} Else{std::vector<int > a;a=v_action.at (0); int sprite_tag=a.at (0); int action_tag=a.at (1); STD::VECTOR<STD:: Vector<int>>::iterator it = V_action.begin (); v_action.erase (it); Run_action (Sprite_tag,action_tag);}}

If there is no data in the container, then there is no action to play, then the Isrunaction is assigned to False, the next time you can play the action directly.

If there is data in the container, we take the data out and delete it before calling Run_action.

Finally, let's see what's inside of run:

sequence* helloworld::run (int action_tag) {switch (Action_tag) {case 1:return sequence::create (moveby::create (0.2, VEC2 (0,100)), Moveby::create (0.2,VEC2 (0,-100)),                            NULL); Default:break;}}

Very simple, is an action, of course, we can increase as needed. (Note that the return parameter is sequence, so you can change the function if you want to play other animations.)


Now look at the effect:


Complete!



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

COCOS2DX several elves play the animation solution in 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.