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 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步