【发布时间】:2017-07-20 05:18:54
【问题描述】:
我有一个层(基础层),上面有几个按钮。有时我想在半透明层上显示模态对话框,当显示时,用户不应该能够点击半透明层下面的任何东西 - 即。他们应该无法点击基础层上的按钮。
那么如何获得一个层来吸收所有这些触摸?现在,如果我单击半透明图层上的任意位置,并且下面的图层上有一个按钮,该按钮会被单击吗?是否有一些必须设置的标志?
【问题讨论】:
标签: cocos2d-x cocos2d-x-3.0 cocos2d-x-3.x
我有一个层(基础层),上面有几个按钮。有时我想在半透明层上显示模态对话框,当显示时,用户不应该能够点击半透明层下面的任何东西 - 即。他们应该无法点击基础层上的按钮。
那么如何获得一个层来吸收所有这些触摸?现在,如果我单击半透明图层上的任意位置,并且下面的图层上有一个按钮,该按钮会被单击吗?是否有一些必须设置的标志?
【问题讨论】:
标签: cocos2d-x cocos2d-x-3.0 cocos2d-x-3.x
您可以为您的图层添加触摸监听器。
void YourLayerYouWantToSwallowTouches::addEvents() {
auto listener = cocos2d::EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [&](cocos2d::Touch* touch, cocos2d::Event* event) {
if (this->getBoundingBox().containsPoint(touch->getLocation())) {
//touchBegan(touch); // You can call touchBegan() for that layer here
return true; // to indicate that we have consumed touch.
}
return false; // we did not consume touch, pass thru.
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
【讨论】: