菜单回调未调用

时间:2015-08-30 17:34:20

标签: c++ cocos2d-x

我想添加几个按钮 - "设置"和"重启"在我的场景中,置于一切之上。我在下面[在我的init()]中执行以下操作:

auto settingsMenu = MenuItemImage::create("settings_gold@2x.png", "settings_white@2x.png",
CC_CALLBACK_1(GameLevelLayer::settingsButtonTapped, this));
settingsMenu->setPosition(Point(440, 280));
this->addChild(settingsMenu);

我还添加了触摸处理程序来处理游戏"动作,"如下:

// handle user touches
auto dispatcher = Director::getInstance()->getEventDispatcher();
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(GameLevelLayer::onTouchBegan, this);
listener->onTouchEnded = CC_CALLBACK_2(GameLevelLayer::onTouchEnded, this);
dispatcher->addEventListenerWithSceneGraphPriority(listener, this);

我的onTouchBegan()和onTouchEnded()被称为正常,但是从不调用settingsButtonTapped(),因为触摸处理程序显然吞下了所有内容。

如何在cocos2d-x 3.x中处理它,以便点击菜单调用settingsButtonTapped()?

谢谢

1 个答案:

答案 0 :(得分:1)

一位同事告知cocos2d-x 3.x方式是使用ui:Button,而不是MenuItemImage。

将代码更改为:

auto settingsButton = cocos2d::ui::Button::create();
settingsButton->setTouchEnabled(true);
settingsButton->loadTextures("settings_gold@2x.png", "settings_white@2x.png");
settingsButton->setPosition(Point(440, 270));
settingsButton->addTouchEventListener(CC_CALLBACK_2(GameLevelLayer::settingsButtonTapped, this));
this->addChild(settingsButton);

// handle user touches
auto dispatcher = Director::getInstance()->getEventDispatcher();
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(GameLevelLayer::onTouchBegan, this);
listener->onTouchEnded = CC_CALLBACK_2(GameLevelLayer::onTouchEnded, this);
dispatcher->addEventListenerWithSceneGraphPriority(listener, this);

现在,按下按钮会调用按钮回调,并且图层上的触摸仍按原样处理。