Hi there,
forgive me for posting such a simple question but had a look around this forum and the google archived one and wasn't able to find a consistent solution.
I'm using 0.7.3 and I'd like to have multiple menuitems share the same callback, but then I can't figure out which menuitem was pressed.
Any ideas?
Thanks!
Menu Callback
(11 posts) (4 voices)-
Posted 2 years ago #
-
This worked putting a tag in the MenuItem, but this was for 0.6? release.
Unfortunatelly the 0.7x release has not this, don't know about 0.8
Posted 2 years ago # -
compare the sender argument from the callback to your menu item instances.
Posted 2 years ago # -
Yeah I read about the tag method but doesn't work in 0.7
I would prefer not to upgrade to 0.8 just because 0.7 works fine for my needs and I'm trying to get the game wrapped up and submitted soon.Posted 2 years ago # -
Thanks Joao.
I'm doing thisfor (id item in mainMenu.children ) if(sender==item) NSLog(@"found");But then how would I check what button it is?
I have the instance of the MenuItemImage, can I check what index it is? Or something else?
This sounds like a stupid question.
Thanks :)Posted 2 years ago # -
Usually I keep a weak reference to the MenuItem in my scene (don't retain it), doing something like this:
@interface MyScene : Scene
{
MenuItem* itemA;
}@end
@implementation MyScene
-(id) init
{
...
itemA = [MenuItemWhatever initBlaBla];
Menu* menu = [Menu menuWithItems:itemA, nil]; //or something like this.
[self addChild:menu];
}@end
Then compare like this:
if(sender==itemA)
NSLog(@"found");Posted 2 years ago # -
Ah I see.
I suppose it can become a bit fidgety if you have lots of menuitems like I'm planning to have (about 12, not that many really...).
Still, it works and that's what matters the most :)
Thanks for your help!
PatrickPosted 2 years ago # -
What I did in 0.7.3 was expose selectedItem in Menu class. It's private but easy to change. I populate my Menus from an array, so I just match the array index with the selectedItem index. However when you move to 0.8 selectedItem changes from an int to a MenuItem. I had to change how I handled these selections.
Posted 2 years ago # -
Wouldn't be better sending to riq an enhacement request with the proper code?
Posted 2 years ago # -
He explained that it was not in the spirit of cocos2d to expose it, which I have no problem with. 0.8 returns the actual MenuItem anyway.
Posted 2 years ago # -
Ah ok .. if 0.8 has in it way better.
Thanks
Posted 2 years ago #
Reply
You must log in to post.