Question

How can I add/remove CCMenu when the same button is clicked? I have added some code..

Thanks in advance..

CCMenu *menu;


    if (!isMenuVisible) {

           CCMenuItemSprite *item = [CCMenuItemSprite itemFromNormalSprite: .......];
           menu = [CCMenu menuWithItems:item, nil];
           [self addChild:menu];

} else {

           // [menu cleanup];/// didn't work
           // [menu removeFromParentAndCleanup:YES]; //// didnt work
           // [menu removeAllChildrenWithCleanup:YES];  //// didn't work

}

    isMenuVisible = !isMenuVisible;


}
Was it helpful?

Solution

you probably want to have the top line in your .h file, making the menu an iVar, to that the reference is kept in between successive executions of this code. Set menu to nil after you remove it.

OTHER TIPS

One way - to create two menus. One for show/hide button, another for all buttons that have to be shown/hidden. It is not good way.

Another way is just to add/remove menuitems to the menu. I mean something like that:

- (void) removeItems
{
    for(CCNode* item in _addedItems)
    {
        [item removeFromParentAndCleanup: YES];
    }

    [_addedItems removeAllObjects];
}

- (void) addItems
{
    // create needed items and add them as children 
    // to your menu and add them to _addedItems array
    // to be able to remove added objects
}

Also before using methods like cleanup, check it's code or at least cocos2d documentation. In your case it was comletely useless.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top