COcos2d中多个MenuItem的一个选择器

时间:2011-12-04 13:32:32

标签: iphone xcode4 cocos2d-iphone

//CCMenuItem
    CCMenuItem *starMenuItem = [CCMenuItemImage 
                                itemFromNormalImage:@"Button0000.png" selectedImage:@"Fork0002.png" 
                                target:self selector:@selector(starButtonTapped:)];

    CCMenuItem *starMenuItem2 = [CCMenuItemImage 
                                itemFromNormalImage:@"Button0001.png" selectedImage:@"Button0002.png" 
                                target:self selector:@selector(starButtonTapped:)];
    CCMenuItem *starMenuItem3 = [CCMenuItemImage 
                                 itemFromNormalImage:@"Button0003.png" selectedImage:@"Button0004.png" 
                                 target:self selector:@selector(starButtonTapped:)];
    CCMenuItem *starMenuItem4 = [CCMenuItemImage 
                                 itemFromNormalImage:@"Button0005.png" selectedImage:@"Button0006.png" 
                                 target:self selector:@selector(starButtonTapped:)];
    CCMenuItem *starMenuItem5 = [CCMenuItemImage 
                                 itemFromNormalImage:@"Button0007.png" selectedImage:@"Button0008.png" 
                                 target:self selector:@selector(starButtonTapped:)];
    CCMenuItem *starMenuItem6 = [CCMenuItemImage 
                                 itemFromNormalImage:@"Button0009.png" selectedImage:@"Button0010.png" 
                                 target:self selector:@selector(starButtonTapped:)];
    CCMenuItem *starMenuItem7 = [CCMenuItemImage 
                                 itemFromNormalImage:@"Button0011.png" selectedImage:@"Button0012.png" 
                                 target:self selector:@selector(starButtonTapped:)];

    //CCMenu
    CCMenu *starMenu = [CCMenu menuWithItems:starMenuItem,starMenuItem2,starMenuItem3,starMenuItem4,starMenuItem5,starMenuItem6,starMenuItem7,nil];

    starMenu.position = ccp(winSize.width/2  , 25);
    [starMenu alignItemsHorizontally];
    [self addChild:starMenu];


-(void)starButtonTapped:(id)sender {
if (/*press starMenuItem4*/) {
   /*do this*/
}else if(/*press starMenuItem4*/){
 /* do another thing*/
}
}

我的所有menuItem都有一个名为“”starButtonTapped的公共选择器:“。但是,我如何区分我在选择器中点击了哪个按钮,以便我可以为每个按钮执行不同的操作?我尝试不要选择多个选项他们很乱。谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

您可以使用代码:starMenuItem.tag = 1;

-(void)starButtonTapped:(CCMenuItem*)sender {

  switch(sender.tag) {
   case 1: // starMenuItem pressed 
     /*do this*/
     break;
   case 2: // starMenuItem2 pressed 
     /*do that*/
     break;
  }
}