cocos2d-x 3.2 实现模态对话框

时间:2023-02-08 21:30:38

使用Layer实现对话框,在init函数中添加以下代码

auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [=](Touch *touch, Event *event)
{
return true;
};
listener->setSwallowTouches(true);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

listener->setSwallowTouches(true);不向下传递触摸,比如有两个Layer A和B,A在B上面,触摸A时,事件就不会传递到B,从而实现模态对话框
listener->setSwallowTouches(false);向下传递触摸事件