GameEvent.h:
1 #pragmaOnce2#include"cocos2d.h"3 using_ns_cc;4 5 classgameevent {6 Public:7 //Package Distribution Data8 Static voidDispatchset (std::stringEventName,void*optionaluserdata =nullptr);9 Static voidDispatch (std::stringEventName,void*optionaluserdata =nullptr);Ten Static voidAddEventListener (std::stringEventName,Conststd::function<void(eventcustom*) >&callback); One Static voidRemoveEventListener (std::stringeventName); A};
GameEvent.cpp:
1#include"GameEvent.h"2 3 4 5 voidGameevent::d ispatch (std::stringEventName,void*optionaluserdata) {6Director::getinstance ()->geteventdispatcher ()dispatchcustomevent (EventName, optionaluserdata);7 }8 9 voidGameevent::addeventlistener (std::stringEventName,Conststd::function<void(eventcustom*) >&callback) {TenDirector::getinstance ()->geteventdispatcher ()Addcustomeventlistener (EventName, callback); One } A - voidGameevent::removeeventlistener (std::stringeventName) { -Director::getinstance ()->geteventdispatcher ()removecustomeventlisteners (eventName); the}
COCOS2DX Custom event class encapsulation