Cocos2d-x Mobile game development-Combo action

Source: Internet
Author: User
Tags repetition

actions are often not singular, but complex combinations. We can combine these basic actions in a certain order to form a coherent set of actions. The combined actions include the following categories: order, side-by-side, finite-number repetition, infinite repetition, anti-action, and animation. Animation we will introduce in the next section, we focus on order, juxtaposition, finite number of repetitions, infinite repetition and reactionary

Below we introduce an example of the use of the combination of actions, as shown in this example, is an Action menu scene, select Menu can go into the action scene, in the action scene click the Go button can perform the action of our choice, click the Back button can return to the menu scene.


Let's look at the specific program code, first look at the HelloWorldScene.h file, its code is as follows:

#ifndef __helloworld_scene_h__#define __helloworld_scene_h__ #include "cocos2d.h" #include "MyActionScene.h"                                                                                                                ① typedef enum {                                                                                                                             ②   ksequence = kspawn,   krepeate,   kRepeatForever1, Kreverse   } actiontypes;                                                                                                                             ③class helloworld:public cocos2d::layer{public:    static cocos2d::scene* createscene ();   virtual BOOL init ();       void Onclickmenu (cocos2d::ref* psender);                                                                              ④      Create_func (HelloWorld);}; #endif//__helloworld_scene_h__

The above code is in the ① line is the introduction header file MyActionScene.h. The ②~③ is defined by the enumeration type Actiontypes, which defines 5 constants in the enumeration type Actiontypes, 5 constants corresponding to 5 menu items. Line ④ declares a function that is used to recall a callback when a different menu is selected.

We are more familiar with the appeal code, we will not introduce it here. Let's look at the next scenario, Myactionscene, whose MyActionScene.h code is as follows:

#ifndef __myaction_scene_h__#define __myaction_scene_h__ #include "cocos2d.h" #include "HelloWorldScene.h" class Myaction:public cocos2d::layer{   cocos2d::sprite *sprite; public:       staticcocos2d::scene* createscene ();   virtual BOOL init ();   Create_func (myaction);      void Gomenu (cocos2d::ref* psender);   void Backmenu (cocos2d::ref* psender);         Voidonsequence (cocos2d::ref* psender);                                                                              ①   void Onspawn (cocos2d::ref* psender);   void OnRepeat (cocos2d::ref* psender);   void Onreverse (cocos2d::ref* psender);   void Onrepeatforever (cocos2d::ref* psender);                                                            ②}; #endif//__myaction_scene_h__

The ①~② line code in the. h file is a declaration of 5 callback functions for menu selection.

Myactionscene implementation code MYACTIONSCENE.CCP file, where click the Go button when the function called Myaction::gomenu code is as follows:

void Myaction::gomenu (ref* psender) {      log ("Tag=%i", This->gettag ());   Switch (This->gettag ()) {         caseksequence:             this->onsequence (psender);             break;         Casekspawn:            this->onspawn (psender);             break;         Casekrepeate:            this->onrepeat (psender);             break;         CasekRepeatForever1:            this->onrepeatforever (psender);             break;         Case Kreverse:             this->onreverse (psender);             break;       Default: Break             ;    }}

We call different functions in this function depending on the selection menu.

The Myactionlayer::onsequence code in MYACTIONSCENE.CCP is as follows: void Myaction::onsequence (ref* psender) {Size size = Director::        GetInstance ()->getvisiblesize ();    POINTP = Point (SIZE.WIDTH/2, 200);                  finitetimeaction* ac0 = (finitetimeaction*) sprite->runaction (Place::create (p)); ①finitetimeaction* AC1 = (finitetimeaction*) sprite->runaction (MoveTo         :: Create (2,point (size.width-130, size.height-200))); ②finitetimeaction* AC2 = (finitetimeaction*) sprite->runaction (jumpby                                          :: Create (2,point (8, 8), 6, 3));                ③finitetimeaction* AC3 = (finitetimeaction*) sprite->runaction (blink::create (2,3)); ④finitetimeaction* AC4 = (finitetimeaction*) sprite->runaction (tintby                                                   :: Create (0.5,0,255,255)); ⑤sprite->runaction (sequence::create (AC0,AC1, AC2, AC3, AC4, AC0, NULL)); ⑥}

The code above implements a sequential action demonstration in which the primary class used is sequence,sequence, which is derived from the Actioninterval attribute interval action. The sequence function is to arrange a number of actions sequentially, and then execute them sequentially, in order of precedence. The Code ⑥ line execution Sequence,sequence The CREATE function requires an action array. The Code section ① creates a place action, because the sequence's create function requires an action of type finitetimeaction, so the expression needs to be sprite->runaction (Place::create (p)) Cast to the finitetimeaction* type. A similar code, line ②~⑤, requires a type cast. The ② line code is the creation of the MoveTo action, and the ③ line of code is to create the Jumpby action. The ④ line code is the creation of the blink action. The ⑤ line code is the creation of the Tintby action.

MYACTIONSCENE.CCP in Myactionlayer::onspawn, this function is a function that is called when a parallel action is demonstrated, and its code is as follows:

void Myaction::onspawn (ref* psender) {   Size size = Director::getinstance ()->getvisiblesize ();    POINTP = Point (SIZE.WIDTH/2, n);      Sprite->setrotation (0);                                                                                                          ①        sprite->setposition (p);                                                                                                        ②      finitetimeaction* ac1 = (finitetimeaction*) sprite->runaction (moveto::create (2,point                           , (size.height-100)));                     ③   finitetimeaction* AC2 = (finitetimeaction*) sprite->runaction (Rotateto::create (2, +));       ④      sprite->runaction (spawn::create (Ac1,ac2,null));                                                                      ⑤   }

The above code implements a side-by-side action demonstration, in which the main class used is the Spawn class, which is also inherited from Actioninterval, which is a parallel execution of several actions at the same time, but requires that the action be executed simultaneously. For example: Mobile Flip, change color, change size and so on. The ⑤ Line Code sprite->runaction (Spawn::create (Ac1,ac2,null)) performs a parallel action, an array of action types for the CREATE function. The ① Line code sprite->setrotation (0) sets the sprite rotation angle to remain in its original state. The ② Line code sprite->setposition (p) is the reset sprite position. The ③ line code creates the MoveTo action. The ④ line code creates the Rotateto action.

MYACTIONSCENE.CCP in Myactionlayer::onrepeat, this function is the function that is called when the repeating action is demonstrated, and its code is as follows:

void Myaction::onrepeat (ref* psender) {Size size = Director::getinstance ()->getvisiblesize ();      POINTP = Point (SIZE.WIDTH/2, 200);        Sprite->setrotation (0);      Sprite->setposition (P); finitetimeaction* AC1 = (finitetimeaction*) sprite->runaction (Moveto::create (2,point (size.wid                     th-100, size.height-100)); ①finitetimeaction* AC2 = (finitetimeaction*) sprite->runaction (Jumpby::create (2,point (10,1                                                          0), 20,5)); ②finitetimeaction* AC3 = (finitetimeaction*) sprite->runaction (Jumpby::create (-10,                                                         -10), 20, 3);                                         ③actioninterval* seq = sequence::create (AC1, AC2, AC3, NULL);                                                                              ④sprite->runaction (Repeat::create (seq,3));      ⑤} 

The code above implements a repeating action demonstration, where the primary class used is the repeat class, which is also inherited from Actioninterval. The ① line code is the creation of the MoveTo action. The ② line code is the creation of the Jumpby action. The ③ line code is the creation of the Jumpby action. The ④ line code is the type that creates the sequence Action object Seq,seq actioninterval* or finitetimeaction*. The ⑤ Line Code sprite->runaction (Repeat::create (seq,3)) repeats the sequence of operations 3 times, and the type of the CREATE function parameter is finitetimeaction.

MYACTIONSCENE.CCP in Myactionlayer::onrepeatforever, this function is the function that is called when the infinite repetition action is demonstrated, and its code is as follows:

void Myaction::onrepeatforever (Ref*psender) {Size size = Director::getinstance ()->getvisiblesize ();      Point P = Point (SIZE.WIDTH/2, 500);        Sprite->setrotation (0);           Sprite->setposition (P);                                                                                                         Ccbezierconfigbezier;        ①bezier.controlpoint_1 = Point (0, SIZE.HEIGHT/2);        Bezier.controlpoint_2= Point (Ten,-SIZE.HEIGHT/2);                                                                                  bezier.endposition= Point (10,20);   ②finitetimeaction* AC1 = (finitetimeaction*) sprite->runaction (Bezierby::create (2,bezier));  ③finitetimeaction* AC2 = (finitetimeaction*) sprite->runaction (Tintby::create (0.5,0,                                                                           255, 255));                                      ④finitetimeaction* Ac1reverse = ((actioninterval*) AC1)->reverse ();    ⑤finitetimeaction* ac2repeat = (finitetimeaction*) sprite->runaction (Repeat::crea                                                    Te ((actioninterval*) ac2,4)); ⑥finitetimeaction* AC3 = (finitetimeaction*) sprite->runaction (spawn::creat                                                E (ac1,ac2repeat,null)); ⑦finitetimeaction* AC4 = (finitetimeaction*) sprite->runaction (spawn::creat                                           E (ac1reverse,ac2repeat,null));                                                 ⑧actioninterval* seq = sequence::create (AC3, AC4, NULL);                                                                         ⑨sprite->runaction (repeatforever::create (seq)); ⑩}

The code above implements a repeating action demonstration, where the primary class used is RepeatForever, which also inherits from Actioninterval. The ⑩ Line Code sprite->runaction (repeatforever::create (seq)) is an infinite repeating action, and the type of the CREATE function parameter is finitetimeaction. The code ①~② line is defined for Bezier control points. The ③ line code creates the Bezier action Bezierby. The ④ line code creates the action Tintby. The ⑤ line of code is a reversal action that creates a Bezierby action. The ⑥ line of code is to create a repeating action. The ⑦ and ⑧ lines of code are creating side-by-side actions. The ⑨ Line code is the Create order action.

MYACTIONSCENE.CCP in Myactionlayer::onreverse, this function is the function that is called when the counter action is demonstrated, and its code is as follows:

void Myaction::onreverse (ref* psender) {   Size size = Director::getinstance ()->getvisiblesize ();        POINTP = Point (SIZE.WIDTH/2, +);      Sprite->setrotation (0);        Sprite->setposition (p);      finitetimeaction* AC1 = (finitetimeaction*) sprite->runaction (                                                moveby::create (2,point (40,60)));                                                        ①   action* AC2 = Ac1->reverse ();                                                                                               ②    actioninterval* seq = sequence::create (AC1, AC2, NULL);                                                 ③      sprite->runaction (repeat::create (seq,2));                                                                                    ④   }

The above code implements the reactionary as a demonstration, supporting the sequential action of the counter-order action, the counter-order action is not a class, not all of the action classes support anti-action. The Xxxto class usually does not support anti-action, which is usually supported by the Xxxby class. The ① line of code is to create a move moveby action. The ② line code calls AC1 's reverse () function to perform an inverse action. The ③ Line code is the Create order action. The ④ Line Code sprite->runaction (Repeat::create (seq,2)) is to perform an inverse action.


For more information, please pay attention to Cocos2d-x series book "Cocos2d-x Combat (Volume Ⅰ): C + + development" bookExchange Discussion website: http://www.c ocoagame.net Welcome to join the COCOS2D-X technical discussion group: 257760386,327403678

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.