MenuItems

Menu vs. MenuItems

CCMenu is just a collection of CCmenuItems, and those menu items are what define the various buttons or labels you will have as part of your menu. Since the CCmenu contains all of the menu items, it controls the alignment and position of those items. By default,all of the menu items are placed at the center of the CCmenu. CCMenu contains the features as follows:

  • You can add MenuItem objects to CCMenu in runtime using addChild.
  • But CCMenu only accecpted children are MenuItem objects.

Creating a menu in cocos2d-x is simple.

  • Create a new scene (extending CCScene)
  • Create a CCMenuItem
1CCMenuItem *pCloseItem = CCMenuItemImage::create(
2                                        "CloseNormal.png",
3                                        "CloseSelected.png",
4                                        this,
5                                        menu_selector(HelloWorld::menuCloseCallback) );
  • Add it to the menu
1CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
  • Add it to the scene
1 this->addChild(pMenu, 1);
  • Add a callback method in the menu object (in this case GameScene)
1void GameScene::menuCloseCallback(CCObject* pSender)
2{
3    CCDirector::sharedDirector()->end();
4    exit(0);
5}

That's all, you can use the menu in your game now.

posted @ 2013-04-22 15:06  Jzong  阅读(129)  评论(0编辑  收藏  举报