您是否将菜单作为子级添加到您的比例/当前图层?
在您将其添加为子节点之前,它不会被渲染。
我将创建一个菜单如下:
// create the sprites for button up and button down
CCSprite* startGameSprite = CCSprite::createWithSpriteFrameName("start_button_up.png");
CCSprite* startGameSpriteClick = CCSprite::createWithSpriteFrameName("start_button_down.png");
// create a menu item (button) with the up/down sprites
CCMenuItemSprite* startGameItem = CCMenuItemSprite::create(startGameSprite, startGameSpriteClick, this, menu_selector(MainMenu::menuStartGameCallback));
// create a menu to hold the buttons (remembering to NULL terminate the list)
CCMenu *menu = CCMenu::create(startGameItem, NULL);
// position the entire menu
menu->setPosition(0,0);
// add it as a child (so it appears
this->addChild(menu);
笔记:
menu_selector(MainMenu::menuStartGameCallback)
这告诉CCMenuItemSprite
当检测到触摸时调用哪个函数,并且应该是MainMenu
以下结构类中的函数:
void MainMenu::menuStartGameCallback(CCObject* sender)
{
CCLOG("Hello!");
}
记得在里面声明这个函数MainMenu.h
现在,要向此菜单添加另一个按钮,您只需对上述代码进行添加:
// NEW /////////
CCSprite* informationSprite = CCSprite::createWithSpriteFrameName("info_button_up.png");
CCSprite* informationSpriteClick = CCSprite::createWithSpriteFrameName("info_button_down.png");
CCMenuItemSprite* infoGameItem = CCMenuItemSprite::create(informationSprite, informationSpriteClick, this, menu_selector(MainMenu::menuInfoCallback));
// END NEW //////
CCSprite* startGameSprite = CCSprite::createWithSpriteFrameName("start_button_up.png");
CCSprite* startGameSpriteClick = CCSprite::createWithSpriteFrameName("start_button_down.png");
// create a menu item (button) with the up/down sprites
CCMenuItemSprite* startGameItem = CCMenuItemSprite::create(startGameSprite, startGameSpriteClick, this, menu_selector(MainMenu::menuStartGameCallback));
// create a menu to hold the buttons (remembering to NULL terminate the list)
// NEW - we include the new info item
CCMenu *menu = CCMenu::create(startGameItem, infoGameItem, NULL);
// position the entire menu
menu->setPosition(0,0);
// add it as a child (so it appears
this->addChild(menu);
记住创建回调函数:
void MainMenu::menuInfoCallback(CCObject* sender)
{
CCLOG("Information!");
}