Skip to content

Instantly share code, notes, and snippets.

@sag333ar
Last active September 14, 2016 12:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sag333ar/0fbc36a2d3a1b314894b to your computer and use it in GitHub Desktop.
Save sag333ar/0fbc36a2d3a1b314894b to your computer and use it in GitHub Desktop.
Create Menu with MenuItemFont in Cocos2d-x
void HelloWorld::createMenuFunction()
{
// set the font for menu with text
MenuItemFont::setFontSize(20);
// create first menu item
auto menuItemFirst = MenuItemFont::create("First menu item", CC_CALLBACK_1(HelloWorld::firstMenuButtonTapped, this));
// CC_CALLBACK_1 is a macro which helps to indicate function pointer. Here, on click menu,
// 'firstMenuButtonTapped' method will get invoked
// create second menu item
auto menuItemSecond = MenuItemFont::create("Second menu item", CC_CALLBACK_1(HelloWorld::secondMenuButtonTapped, this));
// create third menu item
auto menuItemThird = MenuItemFont::create("Third", CC_CALLBACK_1(HelloWorld::thirdMenuButtonTapped, this));
// now create a menu with menu items.
auto menuCenter = Menu::create(menuItemStart,menuItemSettings,menuItemHelp, NULL);
// set the position of menu
menuCenter->setPosition(Vec2(visibleSize.width/2.0, visibleSize.height/2.0));
// set the text color for the menu
menuCenter->setColor(Color3B(0.0f, 0.0f, 0.0f));
// set the alignment & padding for the menu.
menuCenter->alignItemsVerticallyWithPadding(AppDelegate::getInstance()->menuFontSize);
// add it to scene-layer
this->addChild(menuCenter, 3);
}
// on click first menu item
void HelloWorld::firstMenuButtonTapped(Ref* pSender)
{
CCLOG("First menu tapped");
}
// on click second menu item
void HelloWorld::secondMenuButtonTapped(Ref* pSender)
{
CCLOG("Second menu tapped");
}
// on click third menu item
void HelloWorld::thirdMenuButtonTapped(Ref* pSender)
{
CCLOG("Third menu tapped");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment