Use of common classes in cocos2d_x_03 _

Source: Internet
Author: User

Cocos2d-x cocos2d C ++ closure callback

I. Use of textfieldttf input box

# Pragma mark-custom method // custom method, add a textfieldvoid textfieldscene: addonetextfield (float X, float y) {textfieldttf * field = textfieldttf :: textfieldwit2d-aceholder ("Enter:", "", 50); field-> setposition (x, y); // Add the genie to the current layer this-> addchild (field ); // 2. instantiate a touch listener object auto listener = eventlistenertouchonebyone: Create (); // bind a closure function when the touch starts; // [] indicates the external object to be passed in. Field // () indicates the parameter listener-> ontouchbegan = [field] (touch * t, event * E) {// getlocation returns an OpenGL coordinate. // If the clicked vertex is in textfield, if (field-> getboundingbox () is allowed (). containspoint (t-> getlocation () {field-> attachwithime ();} else {field-> detachwithime ();} return false ;}; // 3. Get the event distributor and add an event listener to this. That is, listen to the Field object [text input box] Director: getinstance ()-> geteventdispatcher () -> addeventlistenerwithscenegraphpriority (listener, field );}


Ii. Custom class ball, inherited from spriteball. h
Create_func (type) Macro
Ball. cppinit method implementation
You can use a custom class in the home scenario to create an instance object either in the onenter () method or in Init () Method

3, Cocos2d-x memory management reference count recommended: when using, first retain, use release not recommended: delete simplified operation: After the creation is complete, use autorelease (); iv. Menu usage
Method 2: Set the callback function as a closure function. It belongs to the callback function type defined in the latest feature menu item of C ++ 11 as follows:

2nd Methods: Set the callback method for the menu item through the macro
Cc_callback_1 macro
Callback method declaration in. h file
Implementation of callback methods in. cpp files
V. Use of tableview
Import the cocos2d-x extension header file containing cctableview. h
Scenario inherited from datasouce and delegate
The following is the data source method declaration.
The following table view proxy Declaration
Instantiate a tableview
Complete header file for the entire scenario
Implementation of the entire scenario. cpp File
/// Tableviewscene. CPP // 01_cocos2d-x // created by beyond on 14-10-4. # include "tableviewscene. H "scene * tableviewscene: createscene () {// 'Scene 'is automatically released // create a scene auto scene = scene: Create (); // 'player' Automatically releases auto layer = tableviewscene: Create (); // adds a layer to the scene-> addchild (layer ); // return scene for the scenario where the layer is filled;} // In the "init" method, instantiate the bool tableviewscene: Init () {// 1. call the init of the parent class, CPP does not have s Uper, directly write the parent class name if (! Layer: Init () return false; // screen size winsize = Director: getinstance ()-> getvisiblesize (); // Add a tableview size = size (300,300); tableview * TV = tableview: Create (this, size); // set the TV-> setanchorpoint (point (0, 0); // set the position TV-> setposition (winsize. width/2, winsize. height/2); // set the proxy TV-> setdelegate (this); // Add it to this addchild (TV); Return true ;} # pragma mark-data source method // The following are all data source methods // total number of rows ssize_t tableviewscene: numberofcellsintableview (tableview * Table) {return 7 ;} // size of each row tableviewscene: tablecellsizeforindex (tableview * Table, ssize_t idx) {return size (300, 60);} // celltableviewcell * tableviewscene of each row :: tablecellatindex (tableview * Table, ssize_t idx) {// reuse mechanism, which is obtained from the cache pool first and cannot be obtained before tableviewcell * cell = table-> dequeuecell (); labelttf * label; if (cell = NULL) {// create a cell = tableviewcell: Create (); // create a new label Label = labelttf: Create (); // bind the tag label-> settag (5267); // set the font size label-> setfontsize (30); // set the anchorpoint label-> setanchorpoint (point (0, 0); // Add it to the cell-> addchild (Label);} else {// directly retrieve the label in the cell and assign a new label = (labelttf *) cell-> getchildbytag (5267);} STD: String arr [] = {"Baoyu", "Daiyu", "Baochai", "Xiangyun", "Spring ", "Miao Yu", "Qing Wen"}; // set the label content label-> setstring (stringutils: Format ("label % lD, % s", idx, arr [idx]. c_str (); // return the unique Cell Return cell;} # pragma mark-proxy method // The following are all proxy Methods // call void tableviewscene upon clicking :: tablecelltouched (tableview * Table, tableviewcell * cell) {labelttf * label = (labelttf *) cell-> getchildbytag (5267); STD: String content = stringutils :: format ("n rows clicked, content: % s", label-> getstring (). c_str (); MessageBox (content. c_str (), "title ");}


Focus:
Stringutils: Format (); <span style = "color: RGB (57, 57, 57); font-family: 'courier new'; font-size: 24px; line-Height: 32px; Background-color: RGB (245,245,245); "> <strong> method </strong> </span>
<Span style = "color: RGB (57, 57, 57); font-family: 'courier new'; font-size: 24px; line-Height: 32px; background-color: RGB (245,245,245); "> <strong> the returned STD: String object can be passed through c_str () </strong> </span> <span style = "font-family: 'courier new'; Background-color: RGB (245,245,245 ); "> convert the method to a C string </span>

Focus: reuse mechanism
Tablecelltouched is equivalent to didslectedrowatindexpath in IOS



Use of common classes in cocos2d_x_03 _

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.