Show text and pop-up keypad in Cocos2d-X with simple application of CCTextFieldTTF
Zookeeper
After learning a few days of Cocos2d-X today finally can try the cross-platform development of Cocos2d-X, due to the constraints, I will only test the development of Cocos2d-X on the Android platform, today, we will use a simple text to test the Android performance. We need to use the CCTextFieldTTF class, CCTextFieldTTF is a class for displaying text controls. It is used to input text and real text, similar to Static controls and Edit controls in Windows programming.
Program example: Use the TextFieldTTF class to create a text, and touch the text to bring up a soft keyboard
First, create a TextFieldTTF. h header file and add the following code to the header file.
# Ifndef _ TextFieldTTF_H __# define _ TextFieldTTF_H __# include "cocos2d. h "USING_NS_CC; class TextFieldTTF: public CCLayer {public: bool init (); static CCScene * scene (); // used to handle the touch event bool ccTouchBegan (CCTouch *, CCEvent *); // create a text control in the program: CCTextFieldTTF * textEdit; CREATE_FUNC (TextFieldTTF) ;};# endif // _ HELLOWORLD_SCENE_H __
Then add the following code in TextFieldTTF. cpp.
# Include "TextFieldTTF. h "CCScene * TextFieldTTF: scene () {CCScene * scene = CCScene: create (); TextFieldTTF * layer = TextFieldTTF: create (); scene-> addChild (layer); return scene;} bool TextFieldTTF: init () {// initialize the parent class layer CCLayer: init (); // obtain the window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // create a text box // The first parameter: content displayed in the text box // second parameter: font // third parameter: Text Size textEdit = CCTextFieldTTF: textfieldwit2d-aceholder ("Please input your name:", "Arial ", 36); // set the position of the text box textEdit-> setPosition (ccp (winSize. width/2, winSize. height/2); // Add the text box to addChild (textEdit) on the layer; // when the control is touched, the soft keyboard setTouchMode (kCCTouchesOneByOne) is displayed; setTouchEnabled (true ); return true;} bool TextFieldTTF: ccTouchBegan (CCTouch * touch, CCEvent * ev) {// determines whether the control bool isClicked = textEdit-> boundingBox () is in progress (). containsPoint (touch-> getLocation (); // if the control is clicked, if (isClicked) {// The soft keyboard textEdit-> attachWithIME ();} is displayed ();} // return true for receiving a touch message ;}
Program Execution result:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + incubator/MXMPC9wPgo8cD48YnI + cda-vcd4kpha + incubator + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20141010/2014101008184856.png" alt = "\">
Effect after "Please input your name :"
After the program is installed on the mobile phone: