Search code examples
cocos2d-xcocos2d-x-3.0symfony-eventdispatcher

Which EventDispatcher to use in cocos2d ? Node::EventDispatcher or Director::EventDispatcher?


Which EventDispatcher to use in cocos2d ? Node::EventDispatcher or Director::EventDispatcher ? After referencing the official documentation of both Director class and Node class Director Class Reference Cocos2d-x Node Class Reference Cocos2d-x I am a bit confused about, what is the difference between using

Director::getInstance()->getEventDispatcher() // consider this is used inside a class which is derived from Node class

or

this->getEventDispatcher() // consider this refers to a class derived from Node class

Solution

  • Both are same. this->getEventDispatcher() calls CCNode's getEventDispatcher() function. It returns _eventDispatcher.

    _eventDispatcher = _director->getEventDispatcher(); // Check CCNode.cpp
    

    So These 3 are same

    Director::getInstance()->getEventDispatcher()->addEventListenerWith..
    
    this->getEventDispatcher()->addEventListenerWith..
    
    _eventDispatcher->addEventListenerWith..
    

    NOTE: I just checked Cocos2d-x 4.0