i have used different menuItemImages, i want to call a same method on evey menuitems,but how to detect which menuItem has been clicked??Please give me a correct way
how to detect which MenuItemImage is clicked?
(9 posts) (6 voices)-
Posted 3 months ago #
-
The id parameter sender in the called method, holds a pointer to the menu item. Normally you just cast it
NSAssert( [ sender isKindOfClass:[ CCMenuItem class ] ], @"Ups" ); CCMenuItem* item = ( CCMenuItem* )sender;Posted 3 months ago # -
What @Birkemose says. An example:
- (void) menuAction: (id) sender { if (sender == newGameMenuItem) { [self displayDifficultyMenu]; } else if (sender == helpMenuItem) { [self displayHelp]; } else if (sender == creditsMenuItem) { [self displayCredits]; } else if (sender == achievementsMenuItem) { [self displayAchievements]; } else if (sender == settingsMenuItem) { [self displaySettings]; } }Posted 3 months ago # -
iam not getting it..will you please elobrate it..what is newGameMenuItem ??is it menu item?
Posted 3 months ago # -
I always switch on the menu item tags. That way you don't need to keep unnecessary references to the menu items around.
If you do that, @farah, just set the tag property of the menu items and then in your handler have a switch based on the passed menu item's tag. You can either declare the handler's parameter to be a menu item or a generic id that you cast to a menu item, as above.
The other approach is shown above-- when you create them, keep a reference (ivar) to each menu item being checked: newGameMenuItem, helpMenuItem, etc.
I prefer the tag approach, but whatever.
Posted 3 months ago # -
I probably don't use tags as often as would be useful. Thanks for pointing out.
Posted 3 months ago # -
Other than managing reference counting is there something I don't know about the evils of ivars?
Posted 3 months ago # -
I don't think ivars are evil. But, if you were only keeping them to do something like equality checks when you can compare tags with constants, then I'd rather reduce the number of ivars and do that. If there are other reasons to keep the ivars, then that's something to consider. But, even with those cases, I tend to retrieve objects from the stage using getChildByTag rather than an iVar unless it's something that I need a lot of frequent access to, like in an update method or the like.
I wouldn't say one approach is better than the other in the case above, though. Just preference.
Posted 3 months ago #
Reply
You must log in to post.