Cocos2d-x Study Notes (6) -- button
Step 1: Create a cocos2d-win32 application named menu
Step 2: to simplify the code, I deleted the "Hello World" and background code segment in the init () function of HelloWorldScene. cpp. At the same time, I made some buttons to save time.
In HelloWorldScene. cpp, I modified the init () function as follows:
[Cpp]
Bool HelloWorld: init ()
{
Bool bRet = false;
Do
{
//////////////////////////////////////// //////////////////////////////////
// Super init first
//////////////////////////////////////// //////////////////////////////////
CC_BREAK_IF (! CCLayer: init ());
//////////////////////////////////////// //////////////////////////////////
// Add your codes below...
//////////////////////////////////////// //////////////////////////////////
// 1. Add a menu item with "X" image, which is clicked to quit the program.
// Create a "close" menu item with close icon, it's an auto release object.
CCMenuItemImage * pCloseItem = CCMenuItemImage: itemFromNormalImage (
"CloseNormal.png ",
"CloseSelected.png ",
This,
Menu_selector (HelloWorld: menuCloseCallback ));
CC_BREAK_IF (! PCloseItem );
// Place the menu item bottom-right conner.
PCloseItem-> setPosition (ccp (CCDirector: shareddire()-> getWinSize (). width-20, 20 ));
// Create a menu with the "close" menu item, it's an auto release object.
CCMenu * pMenu = CCMenu: menuWithItems (pCloseItem, NULL );
PMenu-> setPosition (CCPointZero );
CC_BREAK_IF (! PMenu );
// Add the menu to HelloWorld layer as a child layer.
This-> addChild (pMenu, 1 );
// Get window size and place the label upper.
CCSize size = CCDirector: shareddire()-> getWinSize ();
// Use an image as a button
CCMenuItemImage * myItems = CCMenuItemImage: itemFromNormalImage (
"Normal.png", "down.png ",
This,
Menu_selector (HelloWorld: myMenuCallback ));
CC_BREAK_IF (! MyItems );
MyItems-> setPosition (ccp (size. width/2, size. height-50 ));
CCMenu * myMenu = CCMenu: menuWithItems (myItems, NULL );
MyMenu-> setPosition (CCPointZero );
CC_BREAK_IF (! MyMenu );
This-> addChild (myMenu, 1 );
// Use text as a button
CCLabelTTF * pLabel = CCLabelTTF: labelWithString ("sceondMenu", "Arial", 40 );
CCMenuItemLabel * pItemLabel = CCMenuItemLabel: itemWithLabel (pLabel,
This, menu_selector (HelloWorld: secondMenuCallback ));
[Cpp]
// The last parameter in itemWithLabel is the button RESPONSE event. to define what happens after a button is clicked, add the response code to the secondMenuCallback response function.
CCMenu * specify condmenu = CCMenu: menuWithItems (pItemLabel, NULL );
PItemLabel-> setPosition (ccp (size. width/2, size. height-150 ));
Extends condmenu-> setPosition (CCPointZero );
This-> addChild (FIG, 2 );
BRet = true;
} While (0 );
Return bRet;
}
We can find that although the button types to be added are different, the steps are similar,
First, define a button to load the corresponding button source (such as image source, label text source...), and set the button position
Then, define a button class to load the button items defined in the previous step, and set the location of the button container.
Finally, add the button to the background.
At the same time, two button response functions are added to the HelloWorldScene. h class.
[Cpp]
Void myMenuCallback (CCObject * pSender );
Void secondMenuCallback (CCObject * pSender );
Code in HelloWorldScene. cpp:
[Cpp]
Void HelloWorld: myMenuCallback (CCObject * pSender)
{
// Add the RESPONSE event by yourself
}
Void HelloWorld: secondMenuCallback (CCObject * pSender)
{
CCScene * scene = CCScene: node ();
CCSprite * pSprite = CCSprite: spriteWithFile ("HelloWorld.png ");
CCSize size = CCDirector: shareddire()-> getWinSize ();
PSprite-> setPosition (ccp (size. width/2, size. height/2 ));
CCLayer * pLayer = CCLayer: node ();
PLayer-> addChild (pSprite );
Scene-> addChild (pLayer );
CCDirector: sharedDirector ()-> replaceScene (scene );
}
Step 3: Compile and run. You can see a button, which is a graphical button (rough) and a text button. Clicking the first button didn't respond at that time because I didn't add any code IN THE RESPONSE event, and clicking the second button showed the iconic background of the cocos2d-x.
Author: wen294299195