为什么我的 MenuItem 不可点击?

Why isn't my MenuItem clickable?

我有一个带有标签和回调集的 MenuItem,但它仍然无法正常工作,我需要做什么?

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);

即使将回调添加到 menu_item 也不会改变任何内容。我需要做什么才能让我的菜单可点击?

问题是 MenuItemLabel 不是 MenuItem 的标签,而是它的子类。所以我想发生的事情是,尽管在屏幕上看到你的标签是 MenuItem 单击并寻找它自己的 callback 并且因为它没有找到 NULL 或任何它不会尝试查看它的任何子项,谁可能拥有它,或者它的大小为 0,因此您永远无法点击它。

无论如何,我不是很清楚细节,只是为了解决这个问题,你需要删除 MenuItem 的实例并且只使用 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);
You can do like this: i checked, its working in cocos2d-x 3.2 .

auto Label = Label::createWithSystemFont("My Label", "fonts/Marker Felt.ttf", 25);
auto BtnItem = MenuItemLabel::create(Label,  CC_CALLBACK_1(HelloWorld::myCallback, this));

Menu* mymenu = Menu::create(BtnItem, NULL);

mymenu->setPosition(Vec2(WinSize.width/2,WinSize.height/2));

this->addChild(mymenu,1);


void HelloWorld::myCallback(Ref* pSender)
{
 CCLOG("Your Callback ");
}