Cocos2d-x V3.2 + Cocos Studio1.6 to implement a simple uibutton click Function
I haven't written a blog for a long time.
I have been studying cocos studio over the past few days. This software can easily design some game interfaces and import them to cocos2dx. Today I will use buttons as an example.
First, open Cocos Studio1.6 and select UIEditor. After Entering, we find that there are many examples, and we will see an example called demologin.
After the selection, we found that, for example, we briefly introduced the following functions:
Here we select the login button and find that the Instance name of this button is login_Button. We will use this name in the Code and save it.
Find the project folder in the SAVE path and open
With these files, we copy the Resources under the Json and Resources folders to the resource folder in cocos2dx.
This json file records the various data of this widget. cocos2dx parses the data and restores the interface. If you are interested, open the study.
After the code stage, I simply created a hellowZ role? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcmxko6y1q8rHztLDx9KqsNHI/bj2zeKyv7XEuaSzzL/keys =" http://www.2cto.com/uploadfile/Collfiles/20141015/20141015085358140.png "alt =" \ "> and include in header files
Okay, the following is the code.
// HelloWorldScene. h # ifndef _ HELLOWORLD_SCENE_H __# define _ HELLOWORLD_SCENE_H __# include "cocos2d. h "// import the three header files. // note that in the properties of this project-> C \ C ++-> General-> Add the paths of the three files to the include directory. # include "cocostudio/CocoStudio. h "# include" extensions/cocos-ext.h "# include" ui/CocosGUI. h "class HelloWorld: public cocos2d: Layer {public: // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d :: scene * createScene (); // Here's a difference. method 'init 'in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init (); // a selector callback void menuCloseCallback (cocos2d: Ref * pSender, cocos2d:: ui: TouchEventType type); // implement the "static create ()" method manually CREATE_FUNC (HelloWorld) ;};# endif/_ HELLOWORLD_SCENE_H __
// HelloWorldScene. cpp # include "HelloWorldScene. h "USING_NS_CC; // This Is The namespace using namespace cocostudio; using namespace ui; Scene * HelloWorld: createScene () {// 'Scene 'is an autorelease object auto scene = scene: create (); // 'player' is an autorelease object auto layer = HelloWorld: create (); // add layer as a child to scene-> addChild (layer); // return the scene return scene;} // on "init" you need t O initialize your instancebool HelloWorld: init () {// 1. super init first if (! Layer: init () {return false;} Size visibleSize = Director: getInstance ()-> getVisibleSize (); Vec2 origin = Director: getInstance () -> getVisibleOrigin (); // obtain our Cocos Studio UIdemo // here we have created a widget, it seems that cocostudio has its own code style and api // some functions can be mixed with cocos2dx. // The widget here is a bit similar to a layer, I understand the ui as follows: Widget * pNode = cocostudio: GUIReader: getInstance ()-> widgetFromJsonFile ("DemoLogin. json "); this-> addChild (pNode); // you can find the name of the button in the widget and then use the example (a bit like android) ui :: button * button = (ui: Button *) ui: Helper: seekWidgetByName (pNode, "login_Button "); // Add a touch listener for this button (this is different from cocos2dx) button-> addTouchEventListener (this, toucheventselector (HelloWorld: menuCloseCallback); return true ;} // The Listener callback function void HelloWorld: menuCloseCallback (cocos2d: Ref * pSender, ui: TouchEventType) {// cocostudio listener, you can execute the corresponding code according to the type. // The switch is used here to judge. It is very concise and clear: switch (type) {case ui: TouchEventType: TOUCH_EVENT_BEGAN: {log ("touch began"); break;} case ui: TouchEventType: TOUCH_EVENT_MOVED: {log ("touch moved"); break;} case ui: TouchEventType :: TOUCH_EVENT_ENDED: {log ("touch ended"); break;} case ui: TouchEventType: TOUCH_EVENT_CANCELED: {log ("touch canceled"); break;} default: break ;}}
Run
Click the button to test
OK.