Cocos2d-x UI Development Cccontrolbutton Control class instance _c language

Source: Internet
Author: User

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!");
}

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.