The numbers displayed with cclabelttf are not easy to see, so we thought of using images for proxy. Currently, the implementation on the Internet is to make a ccsprite combination of each number. However, I want to dynamically generate textures. If not, you have to manually write one.
Header file
# Ifndef _ x_number_h _ # DEFINE _ x_number_h _ # include <cocos2d. h> # include <xtype. h> namespace cocos2d {// number displayed based on the image/* this class is not a combination of numbers, but rendered into an independent texture ZDH :: xddword is a 64-bit unsigned integer */class ccpicturenumber: Public ccsprite {public: typedef ccsprite inherited; public: ccpicturenumber ();~ Struct (); Virtual bool Init (void); int buildnumber (ZDH: xddword paramnumber, const char * paramnumberresname); int buildnumber (ZDH: xddword paramnumber, cctexture2d * paramtexture ); int buildnumber (ZDH: xddword paramnumber); create_func (numeric); void setnumbertexture (numeric * paramtexture); void setnumbertexture (const char * numeric); interval * getnumbertexture (); ZDH:: xddword getnumber () const; void setnumber (ZDH: xddword paramnumber); int build (); Private: cctexture2d * m_numbertexture; ZDH: xddword m_number;} # endif
Source File
# Include "xpicture_number.h" # include <xstring. h> namespace cocos2d {// construct // build int ccpicturenumber: buildnumber (ZDH: xddword paramnumber, const char * paramnumberresname) from the specified Resource Name) {This-> setnumber (paramnumber); this-> setnumbertexture (cctexturecache: sharedtexturecache ()-> addimage (paramnumberresname); return this-> build ();}// Vertex // build int ccpicturenumber: buildnumber (ZDH: xddword paramnumber, cctexture2d * paramtexture) {This-> setnumber (paramnumber); this-> setnumbertexture (paramtexture ); return this-> build ();} // reset int ccpicturenumber: buildnumber (ZDH: xddw Ord paramnumber) {This-> setnumber (paramnumber); return this-> build ();} // your bool ccpicturenumber: Init (void) {If (! Inherited: Init () return false; return true;} // your ccpicturenumber: ccpicturenumber () {m_numbertexture = nullptr; m_number = 0 ;}// your ccpicturenumber ::~ Else () {If (ZDH: isnotnull (m_numbertexture) {m_numbertexture-> release () ;}// invalid void ccpicturenumber: setnumbertexture (cctexture2d * paramtexture) {If (m_numbertexture = paramtexture) return; If (ZDH: isnotnull (m_numbertexture) {m_numbertexture-> release ();} m_numbertexture = paramtexture; If (ZDH :: isnotnull (m_numbertexture) {m_numbertexture-> retain () ;}/// invalid void plaintext: setnumbertexture (const char * paramnumberresname) {This-> setnumbertexture (cctexturecache :: sharedtexturecache ()-> addimage (paramnumberresname);} // specify cctexture2d * values: getnumbertexture () {return m_numbertexture;} // specify int ccpicturenumber: Build () {If (ZDH: isnull (m_numbertexture) return ZDH: err_fail; ZDH: xansistring strnumber (m_number); // convert the integer to the string int inumcount = strnumber. getlength (); // get the number of characters ccsize stsize = m_numbertexture-> getcontentsize (); // get the texture size. Each number in the texture must be of the same width or height, and arrange int inumwidth = (INT) stsize according to 0123456789. width/10; // The width of each number in the texture. Int inumheight = (INT) stsize. height; // The height of each number in the texture. ccrendertexture * PRT = ccrendertexture: Create (inumwidth * inumcount, inumheight); // create a rendering texture object, and number to determine the width of ccsprite * psprite = ccsprite: Create (); // create a Sprite object to draw numbers psprite-> setanchorpoint (0, 0 ); psprite-> settexture (m_numbertexture); ccrect strect; PRT-> begin (); For (INT I = 0; I <inumcount; I ++) {int inumber = strnumber [I]-'0'; // sets the texture area of the number to be displayed, which is the area strect in paramtexture In the parameter. setrect (inumber * inumwidth, 0, inumwidth, inumheight); psprite-> settexturerect (strect, false, strect. size); psprite-> setposition (I * inumwidth, 0); // calculates the displayed offset position psprite-> visit (); // render to PRT} PRT-> end (); // obtain the generated texture this-> settexture (PRT-> getsprite ()-> gettexture ()); // set the displayed content to strect. setrect (0, 0, inumwidth * inumcount, inumheight); this-> settexturerect (strect, false, strect. size); // by default, the texture obtained through ccrendertexture is inverted. here you need to flip this-> setflipy (true); // release the resource Delete psprite; delete PRT; return ZDH: err_ OK;} // export ZDH: xddword ccpicturenumber: getnumber () const {return m_number;} // export void ccpicturenumber: setnumber (ZDH:: xddword paramnumber) {m_number = paramnumber ;}}
Digital Image
Example
CCPictureNumber * pNum = CCPictureNumber::create(); pNum->BuildNumber(1234567, "ui_play_num05.png"); pNum->setPosition(200, 200); pNum->setAnchorPoint(0, 0); this->addChild(pNum, 100);