Reputation: 113
I am looking to add a button to a menu screen, it does not need to be anything complicated, in fact, the easier the better!
The project is a Cocos2d-x project, and here is the existing start button code;
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);
All I want to do is add another button and call it 'Information'
I can create the image.png for it no problem, but if anyone could assist in helping me with what I need to type for the button to appear, for the moment, let's use hard coded values, say for example, to put it in the top left hand corner of the screen if possible?
I guess it needs an action to tell it what to do when pressed, I have code that I can put inside that, but I am not sure what I need to put to call the action, ordinarily it would be a void or IBAction but this is Cocos so I am unfamiliar with it. Would appreciate any assistance! Thanks :) Chris
Upvotes: 0
Views: 3768
Reputation: 7176
Have you added the menu as a child to your scale/current layer?
Until you add it as as a child node it will not be rendered.
I would create a menu as follows:
// 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);
Notes:
menu_selector(MainMenu::menuStartGameCallback)
This tells the CCMenuItemSprite
which function to call when a touch is detected, and should be a function in the class MainMenu
of the following structure:
void MainMenu::menuStartGameCallback(CCObject* sender)
{
CCLOG("Hello!");
}
Remember to declare this function in MainMenu.h
Now, to add another button to this menu you just need to make an addition to the above code:
// 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);
Remembering to create the callback function:
void MainMenu::menuInfoCallback(CCObject* sender)
{
CCLOG("Information!");
}
Upvotes: 3