Disappear pop up on clicking anywhere on window and not let back layer functionality work

I have implemented code to make popup disappear on clicking anywhere on window except for pop up area but the functionality on back layer i.e sprite touch event also works
I want that disappear property should work but sprite click should be disabled
How can i do that

What is your touch event system looking like? Is the pop up a Layer or a Sprite? I would make the pop up a sprite inside a layer. Put a touch Event on the layer that closes it out unless touching inside the designated area. The touches from the Layer should be swallowed so they don’t pass through.

void BaseFlatMenuLayer::addTouchListeners() {
    auto listener = EventListenerTouchOneByOne::create();
    listener->setSwallowTouches(true);
    
    listener->onTouchBegan = [&](Touch* touch, Event* event) {
        return true;
    };
    
    listener->onTouchEnded = [&](Touch* touch, Event* event) {
        this->onTouchEnded(touch, event);
    };
    
    auto dispatcher = this->getEventDispatcher();
    dispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}

void BaseFlatMenuLayer::onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event) {
    Point touchPoint = Director::getInstance()->convertToGL(touch->getLocationInView());
    if (!TT3Math::rectContainsPoint(backBoard->getBoundingBox(), touchPoint)) {
        if (!blockTapOut) {
            this->removeLayer();
        }
    }
}

These are 2 functions from a Layer I built that does something very similar. Where backBoard is a Sprite that contains the menu. Tapping outside removes the whole Layer including the menu that sits in it.

I think this will help you out.

2 ways to do this.

  • set swallow touches to true and then return true.

or

  • event->stopPropogation();

@tdebock
the pop up is a layer in my case and in the background layer their is a menu which needs to be disabled till pop up is their and when pop up disappears it needs to be again enabled

swallow touches does disable the menus…

I have used it and it does swallow the touches
but when i click outisde the pop up area it do gets disappear but also just after disappear swallow touches functionality gets over and we can touch the menu
it gives a feel that both things are working simultaneously

are you using a customised menu? or cocos2d-x menu?

I am using cocos2d-x menu
but sprite in it as menu item is customised

can i see the code?

sorry I can’t share that
and thanks for the replies
i will try implement something