cocos2dx自定义事件类封装

GameEvent.h:

 1 #pragma once
 2 #include "cocos2d.h"
 3 USING_NS_CC;
 4 
 5 class GameEvent {
 6 public:
 7     //封装派发数据
 8     static void dispatchSet(std::string eventName, void *optionalUserData = nullptr);
 9     static void dispatch(std::string eventName, void *optionalUserData = nullptr);
10     static void addEventListener(std::string eventName, const std::function<void(EventCustom*)>& callback);
11     static void removeEventListener(std::string eventName);
12 };

GameEvent.cpp:

 1 #include "GameEvent.h"
 2 
 3 
 4 
 5 void GameEvent::dispatch(std::string eventName, void *optionalUserData) {
 6     Director::getInstance()->getEventDispatcher()->dispatchCustomEvent(eventName, optionalUserData);
 7 }
 8 
 9 void GameEvent::addEventListener(std::string eventName, const std::function<void(EventCustom*)>& callback) {
10     Director::getInstance()->getEventDispatcher()->addCustomEventListener(eventName, callback);
11 }
12 
13 void GameEvent::removeEventListener(std::string eventName) {
14     Director::getInstance()->getEventDispatcher()->removeCustomEventListeners(eventName);
15 }