for-loopcocos2d-iphonemenuitemccmenuitem

cocos2d for each CCMenuItem


Im setting up a CCMenu, with menu Items like so:

    CCLabelTTF *label1 = [CCLabelTTF labelWithString:@"level 1" fontName:@"Marker Felt" fontSize:32];
    CCLabelTTF *label2 = [CCLabelTTF labelWithString:@"level 2" fontName:@"Marker Felt" fontSize:32];
    CCLabelTTF *label3 = [CCLabelTTF labelWithString:@"level 3" fontName:@"Marker Felt" fontSize:32];
    CCLabelTTF *label4 = [CCLabelTTF labelWithString:@"level 4" fontName:@"Marker Felt" fontSize:32];

    CCMenuItem *level1 = [CCMenuItemLabel itemWithLabel:label1 target:self selector:@selector(levelSelect:)];
    level1.tag = 1;
    CCMenuItem *level2 = [CCMenuItemLabel itemWithLabel:label2 target:self selector:@selector(levelSelect:)];
    level2.tag = 2;
    CCMenuItem *level3 = [CCMenuItemLabel itemWithLabel:label3 target:self selector:@selector(levelSelect:)];
    level3.tag = 3;
    CCMenuItem *level4 = [CCMenuItemLabel itemWithLabel:label4 target:self selector:@selector(levelSelect:)];
    level4.tag = 4;
    CCMenu *menu = [CCMenu menuWithItems:level1, level2, level3, level4, nil];
    [menu alignItemsVerticallyWithPadding:20];

Is there a way I can sewt up a for loop to go through each CCMenuItem? I want to basically want to set enabled to no if its tag is less than something else. For example:

for (ALL MENU ITEMS) {
    if (THAT MENU ITEM.tag < 4) {
        THAT MENU ITEM.enabled = NO;
    }
}

Thanks in advance


Solution

  • CCMenuItems are added as children of your CCMenu, so in order to iterate through them, you could do.-

    for (CCMenuItem *item in [menu children]) {
        if (item.tag < 4) {
            item.enabled = NO;
        }
    }