[Cocos2dx development skills 10] cocosStudio integration and new features of c ++ 11, cocos2dx Official Website
Forward, please stay at address: http://blog.csdn.net/stalendp/article/details/38880997
I haven't touched cocos2dx for a long time, and recently used it again. It took me several hours to get familiar with it again. I found that many new features are worth writing articles. Well, start with the most common ones. I recently used windows and cocosStudio. Let's start with this article. Let's take a look at the creation of the project and the features of c ++ 11. Previously developed and used the cocosBuilder development interface (see this article for integration ).
Preparations
1) Prepare tools such as NDK, andrdoid-SDK, and Visual Studio. Set environment variables such as NDK_ROOT, ANDROID_SDK_ROOT, and Path.
2) install Python 2.7.x (not 3. x. x );
3) download cocos2d-x-3.2 compressed package
Create a game project
Open the command line tool, switch to the cocos2d-x-3.2 directory, and execute the command:
1) setup. py, used to set Environment Variables
2) cocos new circleTheLine-p com. evilgame. circleTheLine-l cpp-d x: \ your \ project \ path, used to create a project
2) After the command is executed, the following content appears in the directory x: \ your \ project \ path. Click the Visual Studio project under proj. win32 to open the project.
Use CocosStudio to create an interface
I only create a page here, so I only select the UI Editor to make it.
The created interface is as follows:
Export the project to the newui_1.json, test.png, and test. plist files. Copy the three files to the Resources directory under the game root directory.
Integration with cocos2dx
The goal is to change the text on the interface after you click "test.
1) Add the corresponding lib project.
Because the use of cocosStudio, need to rely on libCocosStudio, libGUI, libExtensions three libraries, the specific path is as follows (D: \ Program Files \ cocos2d-x-3.2 \ is the root directory)
LibCocosStudio |
D: \ Program Files \ cocos2d-x-3.2 \ cocos \ editor-support \ cocostudio |
LibGUI |
D: \ Program Files \ cocos2d-x-3.2 \ cocos \ ui \ proj. win32 |
LibExtensions |
D: \ Program Files \ cocos2d-x-3.2 \ extensions \ proj. win32 |
You need to modify Visual Studio and reference them as follows:
1a) Add the above three projects to the "solution.
Right-click the project and select Add/existing items from the pop-up menu, as shown below:
1b) Add reference dependency: (solve the link problem)
Right-click the project, select Properties from the pop-up menu, and perform the following operations in the pop-up dialog box:
1c) Add the header file: (solve the compilation problem)
#include "cocostudio/CocoStudio.h"#include "ui/CocosGUI.h"USING_NS_CC;using namespace cocostudio;using namespace ui;
Note that the namespace ui is actually a sub-namespace of cocos2d. Therefore, you need to write it under USING_NS_CC or:
using namespace cocos2d::ui;
If the above path cannot be found, follow the method below to check whether the corresponding path is included.
Right-click the project, select Properties from the pop-up menu, and perform the following operations in the pop-up dialog box:
Here @ {EngineRoot} is a macro variable used to specify the root directory of cocos2dx (I am here D: \ Program Files \ cocos2d-x-3.2 \); the header file is relative to the "add include directory" settings here. Make corresponding adjustments to ensure the correct path. 2) display the interface:
Node = GUIReader: shareReader ()-> widgetFromJsonFile ("NewUi_1.json"); // The node here is the class variable if (node = nullptr) {return true ;} this-> addChild (node); // Of course, to read the scenario file, you can use the following statement // CCNode * pNode = SceneReader: sharedSceneReader () -> createNodeWithSceneFile ("SceneEditorTest/SceneEditorTest. json ");
3) button binding event:
Button * button = static_cast <Button *> (node-> getChildByName ("btnTest"); button-> addTouchEventListener ([&] (Ref * sender, Widget: TouchEventType type) {if (type = Widget: TouchEventType: ENDED) {// note the node lifecycle problem TextBMFont * nn = static_cast <TextBMFont *> (node-> getChildByName ("displayMsg ")); nn-> setString ("msg changed !! ");}});
The above Code uses the lambda expression feature of C ++ 11, which will be introduced below.
C ++ 11 feature -- lambda expressions c ++ can finally use lambda expressions! This will bring great convenience to coding, and the above sample code is well reflected. 1) Comparison of the three methods: Writing Method 1 (not using lambda expressions ):
auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback,this)); void HelloWorld::menuCloseCallback(Object* sender) { Director::getInstance()->end(); }
Method 2 (lambda function pointer ):
Auto callEnd = [] (Object * sender) {Director: getInstance ()-> end (); // Add the code to be called by the button directly here }; auto closeItem = MenuItemImage: create ("CloseNormal.png", "CloseSelected.png", allEnd );
Write 3 (lambda expression ):
Auto closeItem = MenuItemImage: create ("CloseNormal.png", "CloseSelected.png", [] (Object * sender) {Director: getInstance ()-> end (); // Add the code to be called by the button directly here });
If you want to set the expression to access external variables, you can write & OR = in [] with the variable name, where & Indicates access by reference and = Indicates access by value, variables are separated by commas (,). For example, [= factor, & total] indicates that the variable factor is accessed by value, while the variable total is accessed by reference. If it is a reference,
Note the lifecycle of the object.The lifecycle of the callback function is the same as that of the control. For more technology sharing and discussion, add the following group: 385121586
Reference: cocosStudio official Tutorial: Workshop
ExtensionTest/CocoStudioSceneTest/SceneEditorTest. cpp in the official example
Q: How difficult is java cocos2dx game development and learning curve?
There must be some risks ~ Another two problems were solved. One was that no game was ever done ~ The entire game logic may be faulty ~ The other is that c ++ is unfamiliar. If the project is a little bigger, the memory may be leaked ~ Cocos2d-x must be used to do mobile games ~ In case of leaks, the entire program may crash ~ I am afraid of this
Problems with cocos2dx android Development
If you do not know the specific situation, I can import them normally.