123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import { EVENT_TYPE } from "./gameEnum";
- /**事件管理类 */
- class GameEventManager {
- /**单例 */
- private static _instance: GameEventManager = null;
- /**获取事件管理器单例 */
- static get instance(): GameEventManager {
- if (this._instance == null) {
- this._instance = new GameEventManager();
- }
- return this._instance;
- }
- /**事件类实例 */
- private e: cc.EventTarget = null;
- constructor() {
- this.e = new cc.EventTarget();
- }
- /**
- * 注册事件
- * @param type 事件类型
- * @param callback 事件回调
- * @param target 绑定对象
- * @param useCapture 拦截事件
- */
- on<T extends Function>(type: EVENT_TYPE, callback: T, target?: any, useCapture?: boolean): T {
- return this.e.on(type, callback, target, useCapture);
- }
- /**
- * 注销事件
- * @param type 事件类型
- * @param callback 事件回调
- * @param target 绑定对象
- */
- off(type: EVENT_TYPE, callback: Function, target?: any) {
- this.e.off(type, callback, target);
- }
- /**
- * 发送事件
- * @param key 事件类型
- * @param arg1 参数1
- * @param arg2 参数2
- * @param arg3 参数3
- * @param arg4 参数4
- * @param arg5 参数5
- */
- emit(key: EVENT_TYPE, arg1?: any, arg2?: any, arg3?: any, arg4?: any, arg5?: any) {
- this.e.emit(key, arg1, arg2, arg3, arg4, arg5);
- }
- }
- export default GameEventManager.instance;
|