EventListenerCustom was not remove dproperly

Hi guys,
I add a new custom event listener by using the statement code below:

    EventListenerCustom* eventCus = Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_AFTER_SET_NEXT_SCENE, [&](EventCustom *event) {
        Director::getInstance()->getEventDispatcher()->removeEventListener(eventCus);
        doSomething();
    });

But it seems eventCus was not removed when using “removeEventListener” function. The callback keep being called after I switch to another scene.
Does anyone meet this issue before? Please give me some advice.
Thank you!

Try adding it to a node or scene like this. Then it will be removed on its own

  EventListenerCustom *event = EventListenerCustom::create("igm", [=](EventCustom *c){
       doSomething();
            
        });
        
        _eventDispatcher->addEventListenerWithSceneGraphPriority(event, this);

Otherwise, doing it your way you will have to call the following to remove it

 Director::getInstance()->getEventDispatcher()->removeCustomEventListeners(Director::EVENT_AFTER_SET_NEXT_SCENE);
1 Like

Thanks for your replying. It really helps me to solve my problem.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.