【问题标题】:Why isn't my MenuItem clickable?为什么我的 MenuItem 不可点击?
【发布时间】:2015-01-04 05:43:53
【问题描述】:

我有一个带有标签和回调集的 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 也不会改变任何内容。我需要做什么才能让我的菜单可点击?

【问题讨论】:

    标签: c++ cocos2d-x cocos2d-x-3.0


    【解决方案1】:

    问题在于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);
    

    【讨论】:

      【解决方案2】:
      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 ");
      }
      

      【讨论】:

      • 小心,如果这甚至编译,您将覆盖名称 LabelMenu
      • @TankorSmash 你能详细说明你想说什么吗?我不明白你的意思。
      • 您将Menu 重新定义为变量名,但它最初是一个类。这就像在做int int = 1
      • @TankorSmash 所以根据你的说法,我可以直接使用我的 MenuitemLabel 而不将它添加到我的菜单中.. 是这样吗!!!?
      • 所以如果我只是将我的菜单名称替换为其他名称然后菜单.. 对吗?像 Menu *mymenu 而不是 Menu * Menu ...... !!?
      猜你喜欢
      • 1970-01-01
      • 2013-07-15
      • 2023-03-18
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 2016-07-31
      相关资源
      最近更新 更多