MenuItemLabel回调不起作用,从不调用回调,但MenuItemImage有效

时间:2015-08-25 23:49:05

标签: cocos2d-x-3.0

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

Vector<MenuItem*> menuItems;

auto label = Label::createWithTTF("Space Combat", "fonts/Marker Felt.ttf", 24);
label->setPosition(Vec2(origin.x + visibleSize.width/2,
                        origin.y + visibleSize.height - label->getContentSize().height));
this->addChild(label, 1);

auto closeItem = MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(MainMenu::menuCloseCallback, this));
closeItem->setPosition(origin.x + visibleSize.width - closeItem->getContentSize().width/2,
                       origin.y + closeItem->getContentSize().height/2);
menuItems.pushBack(closeItem);

auto playLabel = Label::createWithTTF("Play", "fonts/Marker Felt.ttf", 24);
playLabel->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height *2));
auto playItem = MenuItemLabel::create(playLabel, CC_CALLBACK_1(MainMenu::menuPlayCallback, this));
menuItems.pushBack(playItem);

auto menu = Menu::create();
menu->addChild(closeItem);
menu->addChild(playItem);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);


auto sprite = Sprite::create("HelloWorld.png");
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
this->addChild(sprite, 0);

return true;

}

上面的代码会在单击MenuItemImage时调用menuCloseCallback,但不会对MenuItemLabel执行相同的操作。

1 个答案:

答案 0 :(得分:2)

想出来。我正在定位标签,但菜单项被放置在默认位置0,0 anchor 0,0。一旦我添加了标签并定位了菜单项,一切都很顺利。