How to create a transparent layer that absorbs touches or prevents touches getting through?

106 views Asked by At

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 ?

1

There are 1 answers

0
Darvas On

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);
}