cocos2dx-3.X中对事件处理分析(2)

前端之家收集整理的这篇文章主要介绍了cocos2dx-3.X中对事件处理分析(2)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. 上一篇中,我们大致分析了一下事件处理的整个概括,这一篇,我们来针对EventDispatcher这个核心的事件处理类中的一些函数进行分析,主要涉及到事件的添加,处理, 移除。
  2. 1
  3. 添加事件:
  4. 1.1.
  5. /** Adds a event listener for a specified event with the priority of scene graph.
  6. * @param listener The listener of a specified event.
  7. * @param node The priority of the listener is based on the draw order of this node.
  8. * @note The priority of scene graph will be fixed value 0. So the order of listener item
  9. * in the vector will be ' <0,scene graph (0 priority),>0'.
  10. */
  11. //注释部分说的很明确,用这个函数,优先级固定为0
  12. void addEventListenerWithSceneGraPHPriority(EventListener* listener,Node* node);
  13. -------》》》
  14. void EventDispatcher::addEventListenerWithSceneGraPHPriority(EventListener* listener,Node* node)
  15. {
  16. CCASSERT(listener && node,"Invalid parameters.");
  17. CCASSERT(!listener->isRegistered(),"The listener has been registered.");
  18. if (!listener->checkAvailable())
  19. return;
  20. listener->setAssociatedNode(node);
  21. listener->setFixedPriority(0); //这里就是对应上面的注释,优先级为0
  22. listener->setRegistered(true);
  23. addEventListener(listener); //添加监听器
  24. }
  25. 1.2.
  26. 我们再来看另外一个添加函数
  27. /** Adds a event listener for a specified event with the fixed priority.
  28. * @param listener The listener of a specified event.
  29. * @param fixedPriority The fixed priority of the listener.
  30. * @note A lower priority will be called before the ones that have a higher value.
  31. * 0 priority is forbidden for fixed priority since it's used for scene graph based priority.
  32. */
  33. //这个添加函数需要传入一个优先级,但是最终调用的也是addEventListener函数
  34. void addEventListenerWithFixedPriority(EventListener* listener,int fixedPriority);
  35. 1.3.添加Custom事件的函数
  36. /** Adds a Custom event listener.
  37. It will use a fixed priority of 1.
  38. * @param eventName A given name of the event.
  39. * @param callback A given callback method that associated the event name.
  40. * @return the generated event. Needed in order to remove the event from the dispather
  41. */
  42. EventListenerCustom* addCustomEventListener(const std::string &eventName,const std::function<void(EventCustom*)>& callback);
  43. -→>>>
  44. EventListenerCustom* EventDispatcher::addCustomEventListener(const std::string &eventName,const std::function<void(EventCustom*)>& callback)
  45. {
  46. EventListenerCustom *listener = EventListenerCustom::create(eventName,callback);
  47. addEventListenerWithFixedPriority(listener,1); //最终还是调用addEventListener(listener);方法,下面我们就来看下这个方法
  48. return listener;
  49. }
  50. 1.4
  51. 既然所有的添加事件,最终都会调用addEventListener函数,我们就去这个函数中看一下:
  52. /** Adds an event listener with item
  53. * @note if it is dispatching event,the added operation will be delayed to the end of current dispatch
  54. * @see forceAddEventListener
  55. */
  56. void addEventListener(EventListener* listener);
  57. ----→>>>
  58. void EventDispatcher::addEventListener(EventListener* listener)
  59. {
  60. if (_inDispatch == 0)
  61. {
  62. forceAddEventListener(listener); //主要是这个函数
  63. }
  64. else
  65. {
  66. _toAddedListeners.push_back(listener); //这个是个临时存储
  67. }
  68.  
  69. listener->retain();
  70. }
  71.  
  72. --→>>>我们进入这个函数中去看下:
  73. void EventDispatcher::forceAddEventListener(EventListener* listener)
  74. {
  75. EventListenerVector* listeners = nullptr;
  76. //每一种事件都有一个特定的listenerID,其实就是一个字符串标记,如
  77. const std::string EventListenerTouchOneByOne::LISTENER_ID = "__cc_touch_one_by_one";,根据这个标记,即可以对所有的事件进行分类
  78. 每一种分类都会记录在下面这个map中。
  79. /** Listeners map */
  80. std::unordered_map<EventListener::ListenerID,EventListenerVector*> _listenerMap;
  81. EventListener::ListenerID listenerID = listener->getListenerID();
  82. auto itr = _listenerMap.find(listenerID);
  83. if (itr == _listenerMap.end())
  84. {
  85. listeners = new (std::nothrow) EventListenerVector();
  86. _listenerMap.insert(std::make_pair(listenerID,listeners));
  87. }
  88. else
  89. {
  90. listeners = itr->second;
  91. }
  92. listeners->push_back(listener); //假如到分类的列表中
  93. //这里对优先级为0的进行了特殊处理,刚才在上面有个添加函数,优先级固定为0,这里对他要进行一些特殊处理
  94. if (listener->getFixedPriority() == 0)
  95. {
  96. setDirty(listenerID,DirtyFlag::SCENE_GRAPH_PRIORITY);
  97. auto node = listener->getAssociatedNode();
  98. CCASSERT(node != nullptr,"Invalid scene graph priority!");
  99. associateNodeAndEventListener(node,listener);
  100. if (node->isRunning())
  101. {
  102. resumeEventListenersForTarget(node);
  103. }
  104. }
  105. else
  106. {
  107. setDirty(listenerID,DirtyFlag::FIXED_PRIORITY);
  108. }
  109. }
  110.  
  111. 注意:
  112. EventListenerCustom* addCustomEventListener(const std::string &eventName,const std::function<void(EventCustom*)>& callback);
  113. 在使用上面这个函数添加Custom事件时,Custom事件的ListenerID就是传进来的eventName函数

猜你在找的Cocos2d-x相关文章