0

我有一个层(基础层),上面有几个按钮。有时我想在半透明层上显示模态对话框,当显示时,用户不应该能够点击半透明层下面的任何东西 - 即。他们应该无法单击基础层上的按钮。

那么如何获得一层来吸收所有这些触摸呢?现在,如果我单击半透明图层上的任意位置,并且下面的图层上有一个按钮,该按钮会被单击吗?是否有一些必须设置的标志?

4

1 回答 1

2

您可以为您的图层添加触摸侦听器。

 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);
}
于 2017-07-20T09:37:08.363 回答