Search code examples
c++cocos2d-xcocos2d-x-3.0

Why isn't my MenuItem clickable?


I've got a MenuItem with a label and a callback set, and it still isn't working, what do I need to do?

Menu* menu = Menu::create();

Label* lbl = Label::createWithTTF("My Label",  "fonts/Marker Felt.ttf", 25);
MenuItemLabel* item_label = MenuItemLabel::create(lbl);
item_label->setCallback(callback);

MenuItem* menu_item = MenuItem::create();
menu_item->addChild(item_label);

menu->addChild(menu_item);

myLayer->addChild(menu);

Even adding the callback to the menu_item doesn't change anything. What do I need to do to get my menu clickable?


Solution

  • The issue is that MenuItemLabel is not a label for a MenuItem, but a subclass of it. So I guess what happens is that despite seeing your label on screen is that the MenuItem click and looks for it's own callback and since it doesn't finds NULL or whatever it doesn't try to look any of its children, who might have it, or that it's size is 0, so you could never click on it.

    Anyway, I'm not exactly clear on the details, only that in order to fix the issue, you need to remove the instance of MenuItem and only use MenuItemLabel:

    Menu* menu = Menu::create();
    
    Label* lbl = Label::createWithTTF("My Label",  "fonts/Marker Felt.ttf", 25);
    MenuItemLabel* item_label = MenuItemLabel::create(lbl);
    item_label->setCallback(callback);
    
    menu->addChild(item_label);
    
    myLayer->addChild(menu);