Create a new scene in Cocos2d-x (VS2017)

To use Cocos2dx to switch scenes, we first need to have different scenes. How to create a new scene:
(1) The first step.
Right-click on the project name and select "Add" ——> "Class".

The following window pops up, we enter the name of the class to be added (that is, the name of this new scene), and confirm.
Insert picture description here
(2) The second step.
At this time, VS has automatically generated the corresponding .h and .cpp files for us, but they are placed in the win32 folder by default. We need to move them to the src folder, otherwise the files related to the new scene cannot be found when compiling.
We long press the left mouse button on the new file and drag it to the src folder.

(3) The third step
Next, we follow the declaration and definition of HelloWorld to initially complete our new scene in newScene.h and newScene.cpp.

(4) Step 4
At this point, we can call our newly created scene in the project. The following is the definition of the callback function to switch scenes.

void HelloWorld::startCallback(Ref* pSender) {
    
    
	auto reScene = newScene::createScene();
	Director::getInstance()->replaceScene(reScene);
}

Common problems:
(1) There is a problem that the newly created header file cannot be included.
Solution:
Right click on the project name. Enter the additional include directory in "Properties" -> "C/C++" -> "General" as shown below. Just add the following statement:

$(ProjectDir);

Insert picture description here

complete.

Guess you like

Origin blog.csdn.net/weixin_45711556/article/details/108417743