diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index 96a4bda8dc..517697e2a9 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -279,11 +279,10 @@ void EventDispatcher::associateNodeAndEventListener(Node* node, EventListener* l else { listeners = new std::vector(); + _nodeListenersMap.insert(std::make_pair(node, listeners)); } listeners->push_back(listener); - - _nodeListenersMap.insert(std::make_pair(node, listeners)); } void EventDispatcher::dissociateNodeAndEventListener(Node* node, EventListener* listener) @@ -311,29 +310,7 @@ void EventDispatcher::addEventListener(EventListener* listener) { if (_inDispatch == 0) { - EventListenerVector* listenerList = nullptr; - - auto iter = _listeners.find(listener->getListenerID()); - if (iter == _listeners.end()) - { - listenerList = new EventListenerVector(); - _listeners.insert(std::make_pair(listener->getListenerID(), listenerList)); - } - else - { - listenerList = iter->second; - } - - listenerList->push_back(listener); - - if (listener->getFixedPriority() == 0) - { - setDirty(listener->getListenerID(), DirtyFlag::SCENE_GRAPH_PRIORITY); - } - else - { - setDirty(listener->getListenerID(), DirtyFlag::FIXED_PRITORY); - } + forceAddEventListener(listener); } else { @@ -343,6 +320,44 @@ void EventDispatcher::addEventListener(EventListener* listener) listener->retain(); } +void EventDispatcher::forceAddEventListener(EventListener* listener) +{ + EventListenerVector* listeners = nullptr; + EventListener::ListenerID listenerID = listener->getListenerID(); + auto itr = _listeners.find(listenerID); + if (itr == _listeners.end()) + { + + listeners = new EventListenerVector(); + _listeners.insert(std::make_pair(listenerID, listeners)); + } + else + { + listeners = itr->second; + } + + listeners->push_back(listener); + + if (listener->getFixedPriority() == 0) + { + setDirty(listenerID, DirtyFlag::SCENE_GRAPH_PRIORITY); + + auto node = listener->getSceneGraphPriority(); + CCASSERT(node != nullptr, "Invalid scene graph priority!"); + + associateNodeAndEventListener(node, listener); + + if (node->isRunning()) + { + resumeTarget(node); + } + } + else + { + setDirty(listenerID, DirtyFlag::FIXED_PRITORY); + } +} + void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node) { CCASSERT(listener && node, "Invalid parameters."); @@ -356,13 +371,6 @@ void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* list listener->setRegistered(true); addEventListener(listener); - - associateNodeAndEventListener(node, listener); - - if (node->isRunning()) - { - resumeTarget(node); - } } void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority) @@ -873,33 +881,9 @@ void EventDispatcher::updateListeners(Event* event) if (!_toAddedListeners.empty()) { - EventListenerVector* listeners = nullptr; - for (auto& listener : _toAddedListeners) { - EventListener::ListenerID listenerID = listener->getListenerID(); - auto itr = _listeners.find(listenerID); - if (itr == _listeners.end()) - { - - listeners = new EventListenerVector(); - _listeners.insert(std::make_pair(listenerID, listeners)); - } - else - { - listeners = itr->second; - } - - listeners->push_back(listener); - - if (listener->getFixedPriority() == 0) - { - setDirty(listenerID, DirtyFlag::SCENE_GRAPH_PRIORITY); - } - else - { - setDirty(listenerID, DirtyFlag::FIXED_PRITORY); - } + forceAddEventListener(listener); } _toAddedListeners.clear(); } diff --git a/cocos/2d/CCEventDispatcher.h b/cocos/2d/CCEventDispatcher.h index 41c98495bd..1bd015b27b 100644 --- a/cocos/2d/CCEventDispatcher.h +++ b/cocos/2d/CCEventDispatcher.h @@ -155,9 +155,18 @@ protected: ssize_t _gt0Index; }; - /** Adds event listener with item */ + /** Adds an event listener with item + * @note if it is dispatching event, the added operation will be delayed to the end of current dispatch + * @see forceAddEventListener + */ void addEventListener(EventListener* listener); + /** Force adding an event listener + * @note force add an event listener which will ignore whether it's in dispatching. + * @see addEventListener + */ + void forceAddEventListener(EventListener* listener); + /** Gets event the listener list for the event listener type. */ EventListenerVector* getListeners(const EventListener::ListenerID& listenerID); diff --git a/cocos/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id b/cocos/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id index 0461b27c7d..a33c70d7bd 100644 --- a/cocos/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id +++ b/cocos/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id @@ -1 +1 @@ -9f5dda004e2659caf1fc9f9032e7b386754c7200 \ No newline at end of file +16af4ad046f4db00af7a229d89c406054d8616c3 \ No newline at end of file diff --git a/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id b/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id index d38d577d06..3ef3e93cf1 100644 --- a/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id +++ b/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id @@ -1 +1 @@ -31ce7e2d174b3b02eb3a30cbc999f1302c70b685 \ No newline at end of file +f0ef8eb2f398001c933c2608c1eb219db7deaebf \ No newline at end of file diff --git a/samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/acts.lua b/samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/acts.lua index 3b1618809a..b7080e57cf 100644 --- a/samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/acts.lua +++ b/samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/acts.lua @@ -1,4 +1,4 @@ -require "Cocos2dStudio" +require "CocoStudio" local TMoveBy = class("TMoveBy") TMoveBy._tag = -1