mirror of https://github.com/axmolengine/axmol.git
Merge pull request #4072 from dumganhar/eventdispatcher-fix
closed #3106: EventListeners can't be removed sometimes
This commit is contained in:
commit
c72927b370
|
@ -446,6 +446,17 @@ void EventDispatcher::removeEventListener(EventListener* listener)
|
|||
{
|
||||
CC_SAFE_RELEASE(listener);
|
||||
}
|
||||
else
|
||||
{
|
||||
for(auto iter = _toAddedListeners.begin(); iter != _toAddedListeners.end(); ++iter)
|
||||
{
|
||||
if (*iter == listener)
|
||||
{
|
||||
_toAddedListeners.erase(iter);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EventDispatcher::setPriority(EventListener* listener, int fixedPriority)
|
||||
|
@ -1041,6 +1052,18 @@ void EventDispatcher::removeEventListenersForListenerID(EventListener::ListenerI
|
|||
_priorityDirtyFlagMap.erase(listenerID);
|
||||
}
|
||||
}
|
||||
|
||||
for(auto iter = _toAddedListeners.begin(); iter != _toAddedListeners.end();)
|
||||
{
|
||||
if ((*iter)->getListenerID() == listenerID)
|
||||
{
|
||||
iter = _toAddedListeners.erase(iter);
|
||||
}
|
||||
else
|
||||
{
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EventDispatcher::removeEventListeners(EventListener::Type listenerType)
|
||||
|
|
|
@ -19,7 +19,8 @@ std::function<Layer*()> createFunctions[] =
|
|||
CL(CustomEventTest),
|
||||
CL(LabelKeyboardEventTest),
|
||||
CL(SpriteAccelerationEventTest),
|
||||
CL(RemoveAndRetainNodeTest)
|
||||
CL(RemoveAndRetainNodeTest),
|
||||
CL(RemoveListenerAfterAddingTest)
|
||||
};
|
||||
|
||||
unsigned int TEST_CASE_COUNT = sizeof(createFunctions) / sizeof(createFunctions[0]);
|
||||
|
@ -665,4 +666,88 @@ std::string RemoveAndRetainNodeTest::title()
|
|||
std::string RemoveAndRetainNodeTest::subtitle()
|
||||
{
|
||||
return "Sprite should be removed after 5s, add to scene again after 5s";
|
||||
}
|
||||
}
|
||||
|
||||
//RemoveListenerAfterAddingTest
|
||||
void RemoveListenerAfterAddingTest::onEnter()
|
||||
{
|
||||
EventDispatcherTestDemo::onEnter();
|
||||
|
||||
auto item1 = MenuItemFont::create("Click Me 1", [this](Object* sender){
|
||||
auto listener = EventListenerTouchOneByOne::create();
|
||||
listener->onTouchBegan = [](Touch* touch, Event* event) -> bool{
|
||||
CCASSERT(false, "Should not come here!");
|
||||
return true;
|
||||
};
|
||||
|
||||
_eventDispatcher->addEventListenerWithFixedPriority(listener, -1);
|
||||
_eventDispatcher->removeEventListener(listener);
|
||||
});
|
||||
|
||||
item1->setPosition(VisibleRect::center() + Point(0, 80));
|
||||
|
||||
auto addNextButton = [this](){
|
||||
auto next = MenuItemFont::create("Please Click Me To Reset!", [this](Object* sender){
|
||||
this->restartCallback(nullptr);
|
||||
});
|
||||
next->setPosition(VisibleRect::center() + Point(0, -40));
|
||||
|
||||
auto menu = Menu::create(next, nullptr);
|
||||
menu->setPosition(VisibleRect::leftBottom());
|
||||
menu->setAnchorPoint(Point::ZERO);
|
||||
this->addChild(menu);
|
||||
};
|
||||
|
||||
auto item2 = MenuItemFont::create("Click Me 2", [=](Object* sender){
|
||||
auto listener = EventListenerTouchOneByOne::create();
|
||||
listener->onTouchBegan = [](Touch* touch, Event* event) -> bool{
|
||||
CCASSERT(false, "Should not come here!");
|
||||
return true;
|
||||
};
|
||||
|
||||
_eventDispatcher->addEventListenerWithFixedPriority(listener, -1);
|
||||
_eventDispatcher->removeEventListeners(EventListener::Type::TOUCH_ONE_BY_ONE);
|
||||
|
||||
addNextButton();
|
||||
});
|
||||
|
||||
item2->setPosition(VisibleRect::center() + Point(0, 40));
|
||||
|
||||
auto item3 = MenuItemFont::create("Click Me 3", [=](Object* sender){
|
||||
auto listener = EventListenerTouchOneByOne::create();
|
||||
listener->onTouchBegan = [](Touch* touch, Event* event) -> bool{
|
||||
CCASSERT(false, "Should not come here!");
|
||||
return true;
|
||||
};
|
||||
|
||||
_eventDispatcher->addEventListenerWithFixedPriority(listener, -1);
|
||||
_eventDispatcher->removeAllEventListeners();
|
||||
|
||||
addNextButton();
|
||||
});
|
||||
|
||||
item3->setPosition(VisibleRect::center());
|
||||
|
||||
auto menu = Menu::create(item1, item2, item3, nullptr);
|
||||
menu->setPosition(VisibleRect::leftBottom());
|
||||
menu->setAnchorPoint(Point::ZERO);
|
||||
|
||||
addChild(menu);
|
||||
}
|
||||
|
||||
void RemoveListenerAfterAddingTest::onExit()
|
||||
{
|
||||
EventDispatcherTestDemo::onExit();
|
||||
}
|
||||
|
||||
std::string RemoveListenerAfterAddingTest::title()
|
||||
{
|
||||
return "RemoveListenerAfterAddingTest";
|
||||
}
|
||||
|
||||
std::string RemoveListenerAfterAddingTest::subtitle()
|
||||
{
|
||||
return "Should not crash!";
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -96,4 +96,13 @@ private:
|
|||
bool _spriteSaved;
|
||||
};
|
||||
|
||||
class RemoveListenerAfterAddingTest : public EventDispatcherTestDemo
|
||||
{
|
||||
public:
|
||||
virtual void onEnter() override;
|
||||
virtual void onExit() override;
|
||||
virtual std::string title() override;
|
||||
virtual std::string subtitle() override;
|
||||
};
|
||||
|
||||
#endif /* defined(__samples__NewEventDispatcherTest__) */
|
||||
|
|
Loading…
Reference in New Issue