Cocos2d-x-3.0 alpha1 with C ++ 11 Exercise 8: dart ninja, use cocostudio to add Widgets

Source: Internet
Author: User

Qianniu exercises before seven, are using the cocos2d-x-3.0 alpha0 version of the exercise, began to use cocostudio, only to find the updated alpha1 version, then the first seven articles of the code one-by-one check get rid. Currently, I still don't know which new method to replace with setTouchEnabled. The overall feeling is that alpha1 is closer to alpha0's refactoring. For example, the ccTouchesEnded method has also been changed to onTouchesEnded, which is easier for beginners to understand and remember. 1. Install cocostudio and install cocostudio by default. Run. Only DataEditor can run. None of the first three can be run. The reason may be that the explicit driver is not strong and DirectDraw cannot be started. If you use Remote Desktop login, even if the directory machine can enable DirectDraw, after NetMeeting is started, the system will automatically disable this acceleration, and the editing functions such as UIEditor will become unavailable. 2. Create a widget and use UIEditor. In cocostudio, add a UIButton and UITextArea. The former tag is 2 and the latter is 3. If you need to click the Button effect, select interactive checkbox. After that, select File-export to export the project as three files, json, plist, and png, and add these three files to xcode Resources. 3. xcode implements code to create a WelcomeScene hfile and cpp file. Add the cpp file to xcode and the tool will automatically generate an hfile. Modify the hfile as follows: # ifndef _ cocos2dx3sample _ WelcomeScene __# define _ cocos2dx3sample _ WelcomeScene _ # include "cocos2d. h "# include" cocos-ext.h "# include" cocos-ext.h "# include" cocos/gui/CocosGUI. h "# include" cocostudio/CocoStudio. h "# include" gui/CocosGUI. h "USING_NS_CC; USING_NS_CC_EXT; using namespace cocostudio; using namespace gui; class WelcomeScene: public cocos2d: LayerColor {public: bool init (); CREATE_F UNC (WelcomeScene); void touchButton (Object * obj, TouchEventType eventType); static Scene * scene () ;};# endif/* defined (_ cocos2dx3sample _ WelcomeScene __) */touchButton is an event handle function that will be called when a button is clicked. Modify WelcomeScene. cpp: # include "WelcomeScene. h "# include" HelloWorldScene. h "Scene * WelcomeScene: scene () {// 'Scene 'is an autorelease object scene * Scene = scene: create (); // 'player' is an autorelease object WelcomeScene * layer = WelcomeScene: create (); // add layer as a child to scene-> addChild (layer ); // return the scene return scene;} bool WelcomeScene: init () {if (! LayerColor: initWithColor (Color4B (255,255,255,255) {return false;} UILayer * layer = UILayer: create (); auto widget = cocostudio: GUIReader: shareReader () -> widgetFromJsonFile ("cocos2d3sampleui_1.ExportJson"); layer-> addWidget (widget); this-> addChild (layer ); UIButton * btn = dynamic_cast <UIButton *> (widget-> getChildByTag (2); // 2 for button btn-> setTitleText ("Start"); btn-> addTouchEventListener (thi S, toucheventselector (WelcomeScene: touchButton); UILabel * txt = dynamic_cast <UILabel *> (widget-> getChildByTag (3 )); // 3 for textarea txt-> setText ("this is a text modified in xcode"); txt-> setColor (Color3B (255,100, 0); return true ;} void WelcomeScene: touchButton (cocos2d: Object * obj, TouchEventType eventType) {switch (eventType) {case gui: TOUCH_EVENT_ENDED: Director: getInstance ()-> replaceScen E (HelloWorld: scene (); break; default: break;} GUIReader uses shareReader to obtain its singleton. Later, the official version may be changed to getInstance (). We use its widgetFromJsonFile API to load and initialize a widget object from a json file. Cocostudio UiEditor creates a UI and loads it in the Code. Use getChildByTag or getChildByName to query the widget and save the widget for use. It is more reasonable to use getChildByName to query controls, and semantics helps code reading and maintenance. The function of cocostudio UIEditor is equivalent to using Flash IDE to UI Flex. TextArea in cocostudio is of the UILabel type in cpp. The auto keyword is a new feature of c ++ 0x. It allows programmers to not specify the variable type in encoding, although c ++ is a strong type language. When auto is used as the Data Type of a variable, the compiler automatically deduce its type based on the initial value of the variable. There are both advantages and disadvantages. It is easy to use and many types are ignored. However, if the type is not clear, for example, the variable is of the int type, it may cause overflow if it is used improperly. Dynamic_cast is significantly different from the forced type conversion of the traditional C style. Except for dynamic_cast, the conversion behaviors are determined during the compilation period. Whether the conversion is successful does not depend on the converted object. Dynamic_cast is not. Dynamic_cast depends on RTTI information. during conversion, dynamic_cast checks whether the converted source object can be converted to the target type. If not, null is returned, which is safe. Someone said that dynamic_cast can be used to implement the QueryInterface function. How can this function be implemented? Toucheventselector is a macro definition used to add event listening to the widget control. Its names are all in lower case and may be modified in the future. Its macro definition is as follows: # define toucheventselector (_ SELECTOR) (SEL_TouchEvent) (& _ SELECTOR), then the code can be modified to: // btn-> addTouchEventListener (this, toucheventselector (WelcomeScene: touchButton); btn-> callback (this, (SEL_TouchEvent) (& WelcomeScene: touchButton); SEL_TouchEvent is defined as typedef void (cocos2d:: Object: * SEL_TouchEvent) (cocos2d: Object *, TouchEventType); The typedef function pointer method is used here, And SEL_TouchEvent is a function Number pointer, representing the control event handle, is a general definition.

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.