如何根据标签禁用按钮?

时间:2011-10-03 15:00:17

标签: ios4 cocos2d-iphone

我需要这个代码的帮助。不确定如何根据标签编写代码来禁用/启用按钮。 我试图使用“[levelMenu setIsEnabled:false];”但所有按钮都被禁用。

//在我的init方法中

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"texture.plist"];
    CCSpriteBatchNode *colorSprites = [CCSpriteBatchNode batchNodeWithFile:@"texture.png"];
[self addChild:colorSprites];

    CCMenu *myMenu = [CCMenu menuWithItems:nil];

    int rows = 2;
    int cols = 4;
    static int padding=20;

    for(int count=0; count < (rows*cols); count++) {

    int index1 = count +1;
    NSString *spritefiles1 = [NSString stringWithFormat:@"sprite%d.png", index1];
    random1 = [CCSprite spriteWithSpriteFrameName:spritefiles1];

        CCMenuItemSprite *levelMenu = [CCMenuItemSprite itemFromNormalSprite:random1 selectedSprite:[CCSprite spriteWithFile:@"Iconselected.png"]   disabledSprite:[CCSprite spriteWithFile:@"Iconlocked.png"] target:self selector:@selector(levelSelected:)];

        int yOffset = padding+(int)(random1.contentSize.width/2-((random1.contentSize.width+padding)*(count/rows))); // or /cols to fill cols first
        int xOffset = padding+(int)(random1.contentSize.width/2+((random1.contentSize.width+padding)*(count%rows))); // or %cols to fill cols first
        levelMenu.position = ccp(xOffset, yOffset);

        levelMenu.tag = count+1;
        myMenu.position =ccp(0,300);
        [myMenu addChild:levelMenu];

        //testing to disable the button
       // [levelMenu setIsEnabled:false];

    }

-(void) levelSelected:(CCMenuItemSprite*) sender {
int level = sender.tag;
NSLog(@"test button number %d",level);
}

1 个答案:

答案 0 :(得分:1)

您可能想阅读这篇文章

In Cocos2d, with buttons on multiple layers, how can I control which button to react to user's touch?

我试过,它对我有用

[_fireButton setIsEnabled:NO];