[SOLVED] How to trigger a method using Menu and MenuItemLabels?

Hello.

I’m trying to create a menu. I’m following this documentation. As for I understand, any kind of MenuItem is capable of triggering a method. I want to click a label and print some text in the console (just to check if it works).

I have the following code:

auto myLabel = Label::createWithTTF("My texttttttt", "fonts/Marker Felt.ttf", 24);
myLabel->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height - myLabel->getContentSize().height - 20));
myLabel->enableGlow(Color4B::YELLOW);
auto myItem = MenuItemLabel::create(myLabel,[&](Ref* sender){ cout << "Hello";});

// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, myItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);

The label appears perfectly but nothing happens if I click it.

Any help is appreciated, thanks.

The touch coordinates are relative to the menu. As you set the menu at 0/0, you would need to touch it at these coordinates.

Set the menu’s position to the position of your starting label and offset the labels relative to the menu.

You should also use the CCLOG macro, instead of iostream. You don’t see any output through iostream.

Thank you very much, problem solved! :smile: