其他分享
首页 > 其他分享> > CocosCreator全局消息事件

CocosCreator全局消息事件

作者:互联网

我的git有代码,如下:

NotificationCenter.ts

import { NotificationName } from "./NotificationName";

/**
 * CocosCreator's global Event/Message Center.
 */
export 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(name: NotificationName, callback: () => void) {
        this.eventTarget.on(name, callback);
    }

    /**
     * Dispatch a notification
     * @param name 
     */
    public emit(name: NotificationName) {
        this.eventTarget.emit(name);
    }

    /**
     * Cancel listen
     * @param name 
     */
    public off(name: NotificationName) {
        this.eventTarget.off(name);
    }
}

NotificationName.ts

export enum NotificationName {
    BattleOver = "BattleOver"
}

参考文档:
https://blog.csdn.net/qq_15020543/article/details/88672575
https://www.byjth.com/ccc/130.html

标签:name,instance,param,eventTarget,CocosCreator,NotificationCenter,事件,NotificationN
来源: https://blog.csdn.net/iningwei/article/details/94007759