Integration of the "COCOS2DX development tips 10" Cocosstudio and the new features of c++11

Source: Internet
Author: User

Forwarding, please keep address: http://blog.csdn.net/stalendp/article/details/38880997

Haven't touched COCOS2DX for a long time, recently used up again, spent several hours to re-familiar, found a lot of new features worth writing articles. Well, let's start with the most common ones. Recently with Windows, using Cocosstudio, start with this, by the way, introduce the creation of the project, as well as the characteristics of c++11. Previously developed using the Cocosbuilder development interface (see this article for related integrations).

Preparatory work

1) Prepare the NDK, Andrdoid-sdk,visualstudio and other tools. and set environment variables Ndk_root,android_sdk_root,path and so on

2) Install Python 2.7.x (not 3.x.x);

3) Download cocos2d-x-3.2 compression pack

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 the project

After the 2 command is executed, the following contents will appear in the X:\your\project\path directory, and the project can be opened by clicking on the VisualStudio Project under Proj.win32.

Creating an interface using Cocosstudio

I'm just doing a page here, so just select UI editor to make it.


The following interfaces are produced:


The project is then exported to get newui_1.json,test.png, test.plist file. Copy the three files to the resources directory under the root of the game.

Integrated COCOS2DX

The goal now is to change the text on the interface when the user taps the "test button".

1) Join 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\ for 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 the VisualStudio to refer to them, as follows:

1a) Add the above three projects to the "solution".

Right-click the item and select Add/existing item in the popup menu as follows:


1b) Add Reference dependency: (fix link problem)

Right-click the project, select Properties from the popup menu, and do the following in the popup dialog box:


1c) Add header file: (fix compilation problem)

#include "cocostudio/cocostudio.h" #include "ui/cocosgui.h" Using_ns_cc;using namespace cocostudio;using namespace UI;
Attentionnamespace UI, which is actually a child namespace of cocos2d; So it needs to be written under USING_NS_CC, or written as:

using namespace Cocos2d::ui;

If the above path cannot be found, follow the methods below to see if the path is included.

Right-click the project, select Properties from the popup menu, and do the following in the popup dialog box:


Where @{engineroot} is a macro variable that specifies the root directory of the COCOS2DX (I am D:\Program files\cocos2d-x-3.2\), and the header file is relative to the "Additional Include Directories" setting here. So make the appropriate adjustments to make sure the path is correct. 2) Display this interface:

node = Guireader::sharereader ()->widgetfromjsonfile ("Newui_1.json"); Node here is the class variable if (node = = nullptr) {return true;} This->addchild (node);

3) Bind button event:

button* button = static_cast<button*> (Node->getchildbyname ("btntest")); Button->addtoucheventlistener ( [&] (ref* sender, Widget::toucheventtype type) {if (type = = widget::toucheventtype::ended) {//Note node's life cycle problem textbmfont* nn = static_cast<textbmfont*> (node- >getchildbyname ("displaymsg")); Nn->setstring ("msg changed!!");});
The above code uses the c++11 lambda expression attribute, which is described below.

c++11 attribute--lambda expression C + + Finally, lambda expression can be used! This will bring great convenience to coding, the above example code, well reflected. 1) Comparison of three methods: 1 (no lambda expression):
Auto Closeitem = menuitemimage::create (                                          "Closenormal.png",                                          "Closeselected.png",                                          cc_callback_1 ( Helloworld::menuclosecallback,this));                                            void Helloworld::menuclosecallback (object* sender)  {      director::getinstance ()->end ();    
Notation 2 (lambda function pointer):
Auto Callend = [] (object* sender)  {      director::getinstance ()->end ();//Add the code to be called directly here   };    Auto Closeitem = menuitemimage::create (                                          "Closenormal.png",                                          "Closeselected.png",                      allend);     
Notation 3 (lambda expression):
Auto Closeitem = menuitemimage::create (                                          "Closenormal.png",                                          "Closeselected.png",                                          [] (object* sender)                      {                          director::getinstance ()->end ();//Add the button to call directly here                      });  
If you want to set an expression to access an external variable, you can write & or = Add a variable name within [], where & is accessed by reference, = = is accessed by value, and the variable is separated by a comma, such as [=factor, &total], which means access to the variable FAC by value Tor, and access total by reference. If it's a quote, you need to be aware of the object's life cycle, the callback function has the same life cycle as the control.
Reference: Cocosstudio Official Tutorial: HTTP://UPYUN.COCIMG.COM/COCOSTUDIO/HELPDOC/V1.0.0.0/ZH/INDEX.HTMLC++11 Features:/http blog.csdn.net/star530/article/details/19913611
Extensiontest/cocostudioscenetest/sceneeditortest.cpp in the official case

Integration of the "COCOS2DX development tips 10" Cocosstudio and the new features of c++11

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.