0

我正在寻找在菜单屏幕上添加一个按钮,它不需要任何复杂的东西,事实上,越简单越好!

该项目是一个Cocos2d-x项目,这里是已有的开始按钮代码;

 fileName = (CCString*)sc->imageDict->objectForKey("GUI_StartButton-image");
    CCSprite *startGameSprite = CCSprite::createWithSpriteFrameName( fileName->m_sString.c_str() );
    CCSprite *startGameSpriteClick = CCSprite::createWithSpriteFrameName( fileName->m_sString.c_str() );
    startGameSpriteClick->setColor( ccc3(128, 128, 128) );
    CCMenuItemSprite *startGameItem = CCMenuItemSprite::create(startGameSprite, startGameSpriteClick, NULL, this, menu_selector(MainMenu::menuStartGameCallback));

    startGameItem->setPosition( GameController::sharedGameController()->getPositionOfUIItem( "GUI_StartButton-image" ) );

    pMenu = CCMenu::create(startGameItem, NULL);

我要做的就是添加另一个按钮并将其称为“信息”

我可以为它创建 image.png 没问题,但是如果有人可以帮助我输入按钮出现所需的内容,目前,让我们使用硬编码值,例如,将其放入如果可能的话,屏幕的左上角?

我想它需要一个动作来告诉它按下时要做什么,我有可以放在里面的代码,但我不确定我需要放什么来调用这个动作,通常它会是一个 void 或 IBAction 但这是 Cocos 所以我对它不熟悉。将不胜感激任何帮助!谢谢 :) 克里斯

4

1 回答 1

3

您是否将菜单作为子级添加到您的比例/当前图层?

在您将其添加为子节点之前,它不会被渲染。

我将创建一个菜单如下:

// 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!");
}
于 2013-05-09T09:38:05.823 回答