How to disable event propagation to lower level layers?

76 Views Asked by At

I use cocos2d-x 3.0 RC1 and I need the following: the topmost layer should get all the events, and should not propagate the events to the layers that are below the topmost layer. I don't know how I can do that. I have tried to figure out something with setTouchEnabled and setSwallowTouches but null result.

Please help. this is very important.

1

There are 1 best solutions below

0
On BEST ANSWER

The only way I have found is to implement this:

auto m_touchListenerOneByOne = EventListenerTouchOneByOne::create();
m_touchListenerOneByOne->setSwallowTouches(true);
m_touchListenerOneByOne->onTouchBegan = CC_CALLBACK_2(IntroView::onBoardTouchBegan, this);

_eventDispatcher->addEventListenerWithSceneGraphPriority(m_touchListenerOneByOne, this);

the code above I wrote in init method of the layer. Also this:

bool IntroView::onBoardTouchBegan(Touch* touch, Event* event)
{
    CCLOG("AAAAAAAAA!");
    return true;
};

When you return true in onBoardTouchBegan you “consume” touch, otherwise if layer don’t consume touches those touches are passed to next Layer by priority.