Mobile Game Development in Cocos2d-x

Source: Internet
Author: User

Mobile Game Development in Cocos2d-x
Action is often not a single, but a complex combination. We can combine the above basic actions in a certain order to form a coherent set of combined actions. Composite actions include sequence, parallel action, limited repetition, unlimited repetition, reactionary action, and animation. We will introduce the animation in the next section. In this section, we will focus on sequence, parallelism, limited repetition, unlimited repetition, and reactionary

Next we will introduce the use of combined actions through an example. This example shows an operation menu scenario. You can select a menu to enter the action scenario, in the action scenario, click the Go button to execute the selected action effect. Click the Back button to return to the menu scenario.


Next let's take a look at the specific program code. First, let's take a 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 = 100,   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__

In the above Code, the first line is to introduce the header file MyActionScene. h. ② ~ ③ Define an enumeration type ActionTypes. Five constants are defined in the enumeration type ActionTypes. These five constants correspond to five menu items. Line 4 declares a function for callback when selecting different menus.

You are familiar with the appeal code, so we will not introduce it here. Next let's take a look at the next scenario MyActionScene. Its 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__

In the. h file ~ ② The Line Code declares five callback functions for menu selection.

The implementation code of MyActionScene is MyActionScene. ccp. The Code of MyAction: goMenu called when you click the Go button 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;    }}

In this function, different functions are called Based on the selected menu.

MyActionScene. the code for MyActionLayer: OnSequence in 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 demonstrates the ordered action. The main class is Sequence, and Sequence is derived from the ActionInterval attribute interval action. Sequence is used to arrange several actions sequentially and then execute them one by one in Sequence. Line 6 of the Code executes Sequence. The create function of Sequence requires an array of actions. The Place action is created in line ① Of the code. Because the create function of Sequence requires an action of the FiniteTimeAction type, the expression sprite-> runAction (Place: create (p) must be used )) forced conversion to FiniteTimeAction * type. Code similar to Section ② ~ All rows require forced type conversion. The Code in line ② is to create a MoveTo action, and the code in line ③ is to create a JumpBy action. The fourth line of code is the Blink creation action. The fifth line of code is to create a TintBy action.

In MyActionScene. ccp, MyActionLayer: OnSpawn is called to demonstrate parallel actions. Its code is as follows:

void MyAction::OnSpawn(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.width- 100, size.height - 100)));                     ③   FiniteTimeAction* ac2 =(FiniteTimeAction*)sprite->runAction(RotateTo::create(2, 40));       ④      sprite->runAction(Spawn::create(ac1,ac2,NULL));                                                                      ⑤   }

The code above demonstrates parallel actions. The main class used is the Spawn class inherited from ActionInterval, which is used to concurrently execute several actions, however, all actions must be executed simultaneously. For example, mobile flip, color change, and size change. The fifth line of code sprite-> runAction (Spawn: create (ac1, ac2, NULL) executes parallel actions, an array of Action types of the create FUNCTION. The first line of code sprite-> setRotation (0) sets the sprite rotation angle to keep the original state. The second line of code sprite-> setPosition (p) is to reset the sprite location. The Code in line ③ creates the MoveTo action. The fourth line of code creates a RotateTo action.

In MyActionScene. ccp, MyActionLayer: OnRepeat is called to demonstrate repeated actions. 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.width- 100, size.height - 100)));                     ①   FiniteTimeAction* ac2 = (FiniteTimeAction*)sprite->runAction(                           JumpBy::create(2,Point(10,10), 20,5));                                                          ②   FiniteTimeAction* ac3 = (FiniteTimeAction*)sprite->runAction(                           JumpBy::create(2,Point(-10,-10),20,3));                                                         ③      ActionInterval* seq = Sequence::create(ac1, ac2, ac3, NULL);                                         ④      sprite->runAction(Repeat::create(seq,3));                                                                                    ⑤   }

The code above demonstrates repeated actions. The main class used here is the Repeat class also inherited from ActionInterval. The Code in line ① is used to create the MoveTo action. Line ② code is used to create a JumpBy action. Line ③ Code creates a JumpBy action. The Code in line ④ creates sequence action objects seq. The seq type is ActionInterval * or FiniteTimeAction *. The fifth line of code sprite-> runAction (Repeat: create (seq, 3) repeats the sequential action three times. The type of the create FUNCTION parameter is FiniteTimeAction.

In MyActionScene. ccp, MyActionLayer: OnRepeatForever is a function called to demonstrate infinite repetition. 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(10, -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::create((ActionInterval*)ac2,4));                                                    ⑥      FiniteTimeAction* ac3 = (FiniteTimeAction*)sprite->runAction(                                       Spawn::create(ac1,ac2Repeat,NULL));                                                ⑦      FiniteTimeAction* ac4 = (FiniteTimeAction*)sprite->runAction(                                       Spawn::create(ac1Reverse,ac2Repeat,NULL));                                           ⑧      ActionInterval* seq = Sequence::create(ac3, ac4, NULL);                                                 ⑨      sprite->runAction(RepeatForever::create(seq));                                                                         ⑩   }

The code above demonstrates repeated actions. The main class used here is RepeatForever, which is also inherited from ActionInterval. The first line of code sprite-> runAction (RepeatForever: create (seq) is to execute an infinite repetition action, and the type of the create FUNCTION parameter is FiniteTimeAction. Code ① ~ (2) The line defines the beiser Curve Control Point. Line ③ code to create the BezierBy curve action. The fourth line of code creates the action TintBy. The fifth line of code is the reverse action of the BezierBy action. The sixth line of code is to create repeated actions. The code in the seventh and second lines is to create a parallel action. The first line of code is to create an ordered action.

MyActionScene. ccp MyActionLayer: OnReverse. This function is called when the Reactionary Work is demonstrated. Its code is as follows:

void MyAction::OnReverse(Ref* pSender){   Size size = Director::getInstance()->getVisibleSize();        Pointp = Point(size.width/2, 300);      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 code above demonstrates how reactionary actions are supported. Reverse actions are not a class, and not all reverse actions support reactionary actions. The XxxTo class usually does not support Reactionary Work, And the XxxBy class usually supports. The line ① code is used to create a mobile MoveBy action. The second line of code calls the reverse () function of AC1. The Code in line ③ is the creation sequence action. The fourth line of code sprite-> runAction (Repeat: create (seq, 2) is to execute Reactionary Work.


More content please pay attention to the Cocos2d-x series of books "Cocos2d-x practice (Volume I): C ++ development" book exchange discussion site: http://www.cOcoagame.netWelcome to cocos2d-x Technology Discussion Group: 257760386, 327403678

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.