In the application development, both the Android operating system and the iOS operating system, its development framework provides controls, including keystrokes, drag sliders, etc., so as to improve the development efficiency. For game development, UI development also requires controls to improve development efficiency. For Cocos2d-x, a number of control classes have been provided from the 2.0 release to help us better develop the UI.
Add the following two sentence codes to the HelloWorld.h
You need to include the following header file and the declaration of the namespace
#include "cocos-ext.h"
using namespace cocos2d::extension;
A function that joins the button event response at the same time
void Touchdown (Ccobject * psender,cccontrolevent evt);
The following is the INIT function in HelloWorld.cpp
Add a new item in the additional include E:\cocos2d-x-2.2\cocos2d-x-2.2\extensions////////////////////////////-> connector-> input
The above steps allow you to use the control class bool Helloworld::init () {bool BRet = false; do {cc_break_if (!
Cclayer::init ());
Create a text class that is the text displayed in the button Cclabelttf * title = Cclabelttf::create ("Music", "Arial", 32);
You can set the title of the Color Title->setcolor (CCC3 (255,0,0)); Create a picture of nine sister (a picture that supports stretching, stretching can avoid picture distortion, can save space)//button under normal picture Ccscale9sprite * Buttonnormal = Ccscale9sprite::create ("
Buttonbackground.png ");
Control class Cccontrolbutton, the first parameter is the button display text, the second parameter is the button in the normal state of the picture//button size and title is the same size, which is why choose nine sister pictures of the reason, will be the size of the title automatically stretching
Cccontrolbutton * button = Cccontrolbutton::create (Title,buttonnormal);
Create a button pressed background picture Ccscale9sprite * spriteselected = ccscale9sprite::create ("Buttonhighlighted.png"); Sets the background picture when the button is pressed, the second parameter is a macro, and F12 view the other macro button->setbackgroundspriteforstate (spriteselected,
cccontrolstateselected); Modify the title's Color Button->settitlecolorforstate (CCC3 (0,255,255), Cccontrolstatehighlighted);
Button->setposition (CCP (240,160));
This->addchild (button);
Add Event button->addtargetwithactionforcontrolevents (This,cccontrol_selector (Helloworld::touchdown) to the button.
Cccontroleventtouchdown);
BRet = true;
while (0);
return bRet; }
On the implementation of event response function
void Helloworld::touchdown (Ccobject * psender,cccontrolevent evt)
{
cclog ("Touch down!");
}