export default class NotificationCenter {
private eventTarget: cc.EventTarget = new cc.EventTarget();
private static instance: NotificationCenter = null;
public static get Instance(): NotificationCenter {
if (this.instance == null) {
this.instance = new NotificationCenter();
}
return this.instance;
}
/**
* Listen to a notification
* @param name
* @param callback
*/
public on<T>(type: string, callback: ($type: string, $data: T) => void, target?: any): void {
this.eventTarget.on(type, callback, target);
}
/**
* Dispatch a notification
* @param name
*/
public dispatch<T>(type: string, data?: T) {
this.eventTarget.emit(type, type, data);
}
public hasEventListener($type): boolean {
return this.eventTarget != null && this.eventTarget.hasEventListener($type);
}
/**
* Cancel listen
* @param name
*/
public off<T>(type: string, callback: ($type: string, $data: T) => void, target?: any): void {
this.eventTarget.off(type, callback, target);
}
}
Cocos Creator自定义事件
最新推荐文章于 2024-02-24 14:32:38 发布