码迷,mamicode.com
首页 > 其他好文 > 详细

Cocos Creator自定义事件

时间:2020-06-19 10:27:42      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:listener   vat   creat   instance   ==   target   his   private   data   

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自定义事件

标签:listener   vat   creat   instance   ==   target   his   private   data   

原文地址:https://blog.51cto.com/aonaufly/2505763

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!