I want to pass parameter(int) while calling jumpScene function in cocos2d-x
CCMenuItem *menuItem1 = CCMenuItemFont::itemFromString(“PlayGame”,this, menu_selector(MainMenuScene::jumpScene));
How to pass parameter?
thank you
I want to pass parameter(int) while calling jumpScene function in cocos2d-x
CCMenuItem *menuItem1 = CCMenuItemFont::itemFromString(“PlayGame”,this, menu_selector(MainMenuScene::jumpScene));
How to pass parameter?
thank you
You can not pass a parameter in MenuItem selector.
What do you want to do?
May be you can use other way to resolve it.
I what to use jumpScene functin pass parameter(id) in order to jump different scenes.
I think you can do it like this:
void MainMenuScene::menuCallback(CCObject * pSender)
{
CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);
int tag = (int)pMenuItem->getTag();
switch (tag)
{
// do different things for different tags
}
}
So you can change menu items’s tag to jump different scenes.
Thanks, Minggo!
I’m not sure if it worked for that intangible guy, but it worked for me.
Hi Minggo,
Using getTag()
did not work for me. I was getting arbitrary values from it. What worked for me was getUserData()
. I am using version 2.0-x-2.0.4
Thanks for the suggestion though.
you should set the tag of your menu item after you create it
//ex
menuItem1.setTag(10);
then you can get its tag
If you want to just know which menu item was activated, then use setTag/getTag, but if you want to actually pass some extra data from the code that creates the menu to the callback method then you can use setUserData/getUserData. Most of the time you probably just want to know which menu item was called and then setting and testing the tag should suffice.
const int kTagPlaygame = 1;
const int kTagOptions = 2;
CCMenuItem *menuItem1 = CCMenuItemFont::itemFromString("Play Game",this, menu_selector(MainMenuScene::jumpScene));
menuItem1->setTag(kTagPlaygame);
CCMenuItem *menuItem2 = CCMenuItemFont::itemFromString("Options",this, menu_selector(MainMenuScene::jumpScene));
menuItem2->setTag(kTagOptions);
// add each item to menu and to the scene
void MainMenuScene::menuCallback(CCObject * pSender)
{
CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);
int tag = (int)pMenuItem->getTag();
switch (tag)
{
case kTagPlaygame:
// replace scene with GameScene
case kTagOptions:
// replace scene with OptionsScene
default:
// log error w/tag
}
}
CCMenuItemSprite *playButton = CCMenuItemSprite::create(GButton::buttonWithText(), NULL, this, menu_selector(Menu::playGame));
i face error. undefine refrences GButton::buttonWithText().
help me thanks
Remove the ()
in the GButtonWithText()