0

I have a layer(base layer) upon which there are several buttons. At times I would like show modal dialogue boxes on a translucent layer, which when displayed, the user should not be able to click on anything below translucent layer - ie. they should not be able to click on the buttons on the base layer.

So how to get a layer to absorb all these touches ? Right now if I click anywhere on the translucent layer, and there is a button on the layer below, the button gets clicked? Is there some flag that must be set ?

Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190

1 Answers1

2

You can add touch listener for your layer.

 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);
}
Darvas
  • 974
  • 3
  • 14
  • 27