Use Coco2d-x2.2.3 version to develop fruit ninja game environment configuration

1. Cocos2d-x-2.2.3 version installation and configuration.

1. In the Cocos2D-X 2.2.3 directory, click cocos2d-win32.vc2010.sln.

Since vs2019 is installed on my computer, it was opened with vs2019 after clicking. After opening, I didn’t think much about it and started compiling. When I finished compiling and starting the project, I reported an error.

错误C1189#error: Macro definition of snprintf conflicts with Standard Library function declaration libcocos2d

The reason is that many libraries or programs define the snprintf() function as _snprintf(), but _snprintf() was not supported before vs2015 appeared. However, VS20 19 defines snprintf().

This obviously leads to the redefinition of snprintf().

The solution is to find the following definition in all your files that define snpritf (maybe you are using a library provided by others and there is a conflict, then you need to modify and recompile the library):

#define snprintf _snprintf    replace it with

#if defined(_MSC_VER) && _MSC_VER<1900

#  define snprintf _snprintf

#endif

For convenience, I directly put the project into vs2010 to compile, and it happened that VS2010 was also downloaded on my computer.

Since it was compiled in VS2019, the following error will be reported when compiling in VS2010

Unable to find build tools for v142 (platform-toolset="v142")

cause of the problem

V142 corresponds to VS2019. The compiler before the project was VS2019, and now it is changed to VS2010, so this error is reported.

v142–>VS2019

v141–>VS2017

v140–>VS2015

v120–>VS2013

Modify it to the tool set corresponding to the current compiler, and then recompile

Project –> Properties –> General –> Platform Toolset –> Select the corresponding platform tool v100 is VS2010

If it still doesn't work, delete the hidden file .vs in the project directory and recompile

2. After the generation is complete, select "HelloCpp" right-click -> set as startup project, then start debugging, and the project will run

3. Use Python to create a new project.

The specific operations are as follows:
(1)Download and install Python2.7 (cocos2d-x only supports python2 and does not support higher versions of python).
(2)Then configure the environment variable and add the python installation path to the environment variable Path.

Open the cmd command prompt and enter python --version to display the following content, indicating that the configuration is successful

Go to the tools/project-creator directory of the COCOS2D installation directory

My path is F:\Cocos2d-x\cocos2d-x-2.2.3\cocos2d-x-2.2.3\tools\

project-creator 

(3) Enter commands with Python

python create_project.py -project FruitNinja-package com.eyu.helloword -language cpp

I successfully created a new project named FruitNinja, the language is c++, and there are all versions , and the project it built is in the projects file folder in the cocos2d-x2.2.3 folder

(4) We enter the newly created folder, and we can see the versions of each platform. My project is to run on a windows computer, so open the .win32 folder, click the .sln file inside, and open it with VS2010

Click to start debugging and find that this project can be run directly

2. Realize the first scene HelloWorld of Fruit Ninja and the second scene PrepaScene

1. Modify the HelloWorld scene

Modify the init() method of the HelloWorld class: create a picture sprite with back.png and add it to the scene as the background    

After modification, the HelloWorld scene is as shown below, with only one background image

3. Realize the second scene of the game

(1) Create a new layer class PrepareScene. This class also has a method to create a static scene. The PrepareScene layer is added to this scene. The implementation of the method is the same as that of HelloWorld::scene

 

class PrepareScene : public CCLayer

{

public:

PrepareScene();

virtual bool init();

static cocos2d::CCScene* scene();

CREATE_FUNC(PrepareScene);



//把主界面的图片精灵添加到场景中

void loadlogo();

void loadtitle();

void loadmenu();



void scle_rotate(int i);   //设置精灵旋转



int index;

private:

#define RING1_ROTATE 1

#define RING2_ROTATE 2

#define RING3_ROTATE 3

#define PEACH_ROTATE 4

#define SANDIA_ROTATE 5

#define BOOM_ROTATE 6



CCSize size;        //记录屏幕大小

bool ifstart;                         //记录主界面状态,是否已经准备好



};

(2)类方法的实现

构造函数,初始化属性



PrepareScene::PrepareScene()

{

index = 0;

angel = 0;

ifstart = false;

}

init函数设置界面的背景图片,init函数中调用了loadlogo()设置界面的其他元素



bool PrepareScene::init()

{

size = CCDirector::sharedDirector()->getWinSize();     //获取屏幕大小

CCSprite* backsp = CCSprite::create("background.jpg"); //用图片创建精灵backsp

backsp->setPosition(ccp(size.width / 2, size.height / 2));  //把锚点设置在屏幕正中央

this->addChild(backsp, -1);                             //背景图片添加到最底层,负数代表最高优先级

loadlogo();

return true;

}

 

The Loadlogo() function realizes adding the banner and logo elements of the main interface. After the banner is successfully added to the scene, call the action callfun through the function and then call the loadtitle method

The implementation of the loadtitle function is similar to that of the loadlogo function, and then the loadmenu function is called

Continuous action CCActionInterval of basic actions

CCJumpTo and CCJumpBy are delayed actions, that is, actions that can be completed after a period of time

CCJumpTo: Jump a certain CCSprite to a certain position
CCJumpBy: Jump a certain CCSprite for a certain distance, it has a method reverse, which makes the object return according to the original path

About To and By:

To: absolute action.

By: relative action.

For example: the coordinates of the current object are (20,20).

After CCMoveTo (50,50), it moves to the position of (50,50).

After CCMoveBy (50, 50), it moves relative to the current coordinates, and the final coordinates are (70, 70).

CCActionInstant inherits from CCFiniteTimeAction and is a continuously executing action class. That is to say, within a certain period of time, an action is executed and completed.

 //Move to the coordinate point after a few seconds

    CCMoveTo::create("time","coordinates");

    CCMoveBy::create("time","coordinates");

    // After a few seconds, after several bounces to the specified position

    CCJumpTo::create("Time","Target Position","Height","Numbers to Goal");

    CCJumpBy::create("Time","Target Position","Height","Numbers to Goal");

Function callback action CCCallFunc

CCCallFunc is also a subclass of momentary action CCActionInstant. It mainly has three function callback action classes. The difference between the three function callbacks is the number of parameters: 0, 1, 2.

 CCCallFunc::create('object this', 'callback function'); //Callback function: without parameters

    CCCallFuncN::create('object this', 'callback function'); //Callback function: passing itself as a parameter (CCNode* node)

    CCCallFuncND::create('object this', 'callback function', 'arbitrary parameter void'); //Callback function: with 2 parameters (CCNode* node, void* a)

Combined action

Combination movements, as the name suggests, are to combine single movements to form a more complex movement.

Such as rotating while moving, executing function callback actions after bouncing, etc...

The class of combined actions is also a subclass of CCActionInterval, which is mainly divided into two categories: sequential actions and repetitive actions.

  1. Sequence actions: the order in which actions are performed.

   CCSpawn::create("action object 1", "action object 2", ..., NULL); //Actions are executed at the same time

   CCSequence::create("action object 1", "action object 2", ..., NULL); //Actions are executed in order

set fruit toss

Variable speed action CCEaseAction

CCEaseAction is also a subclass of CCActionInterval. The characteristic of this type of action is that the speed can be changed during the execution of the action. Such as: CCMoveTo, you can move with acceleration or deceleration, or you can move with acceleration and then deceleration.

The existence of this class is because some actions in the game are not executed uniformly, like a free-falling ball, the falling speed will become faster and faster instead of falling at a constant speed. So the cocos2dx engine encapsulates some commonly used variable speed classes.

Such speed changes can be roughly divided into three types:

(1) In: from slow to fast.

(2) Out: from fast to slow.

(3) InOut: From slow to fast, then slow again.

Among them, the speed change is carried out according to some formulas in physics. Such as sine, exponential, etc.

It is worth noting that what CCEaseAction changes is the speed during the execution of a certain continuous action, and the execution time of the action remains unchanged.

    Among them, the changes about CCEaseIn, CCEaseOut, and CCEaseInOut are a bit complicated.

Loadmenu function: Add three or six sprite elements to the scene and make them rotate at different speeds

void PrepareScene::loadmenu()

{

CCSprite* scle1_dojo = CCSprite::create("dojo.png");

CCSprite* scle2_new = CCSprite::create("new-game.png");

CCSprite* scle3_quit = CCSprite::create("quit.png");

CCSprite* peach = CCSprite::create("peach.png");

CCSprite* sandia = CCSprite::create("sandia.png");

CCSprite* boom = CCSprite::create("boom.png");



scle1_dojo->setPosition(ccp(120, 180));

scle2_new->setPosition(ccp(350, 180));

scle3_quit->setPosition(ccp(550, 130));

peach->setPosition(ccp(120, 180));

sandia->setPosition(ccp(350, 180));

boom->setPosition(ccp(550, 130));



this->addChild(scle1_dojo);

this->addChild(scle2_new);

this->addChild(scle3_quit);

this->addChild(peach);

this->addChild(sandia);

this->addChild(boom);


//旋转效果

scle_rotate(RING1_ROTATE);

scle_rotate(RING2_ROTATE);

scle_rotate(RING3_ROTATE);

scle_rotate(PEACH_ROTATE);

scle_rotate(SANDIA_ROTATE);

scle_rotate(BOOM_ROTATE);


ifstart = true;   //记录主界面已经准备好

}

实现精灵元素旋转的函数

 

(3) After the PrepareScene class is implemented, we modify the functions of the AppDelegate class

CCScene* pScene = PrepareScene::scene();修改为  CCScene* pScene = HelloWorld::scene();

The location of the statement is in the following function body

bool AppDelegate::applicationDidFinishLaunching() {

……

//Create a scene HelloWorld, this is the first interface of the game program

CCScene* pScene = HelloWorld::scene();

    return true;

}

Click Run to see the effect, as shown in the figure below. Then the second game scene is also partially implemented

  • Set scene switching

1. Modify the HelloWorld class and add loadScene( float  dt); method

class HelloWorld : public cocos2d::CCLayer

{

    ……

void loadScene(float dt);

};

void HelloWorld::loadScene(float dt)

{

    CCScene* scene = PrepareScene::scene();

//CCTransitionCrossFade* pScene = CCTransitionCrossFade::create(0.1,scene);

CCDirector::sharedDirector()->replaceScene(scene);

}

loadScene( float dt ); The function replaces the HelloWorld scene with the PrepaScene scene (the menu interface of the game)

2. Undo the modification made to the AppDelegate::applicationDidFinishLaunching function, and return to the HelloWorld.cpp file to modify the init function, add the following line, set a one-time timer, and execute the HelloWorld::loadScene() function to realize the scene after 1.5 seconds switch

bool HelloWorld::init()

{

  ……

    //One-time timer, execute the corresponding refresh body function after a delay of 1.5 seconds, only execute once, and then do not refresh

    this->scheduleOnce(schedule_selector(HelloWorld::loadScene), 1.5f);

    this->setKeypadEnabled(true);

    return true;

}

Guess you like

Origin blog.csdn.net/m0_46785351/article/details/127736296