【Laya】发送和接收事件

UI监听的方法

onShow() {
            ClientEventManager.Instance.On(ClientEvent.UPDATE_GAME_MONEY, this, this.OnUpdataMoney);
           
        }
        // 关闭界面
        onClosed(): void {
            ClientEventManager.Instance.Off(ClientEvent.UPDATE_GAME_MONEY, this, this.OnUpdataMoney);
        }

 事件manager-

class ClientEventManager extends Singleton {
    //---------------instance begin----------------------------------------------------
    public static get Instance():ClientEventManager
    {
        if(!this._instance)
        {
            this._instance = new ClientEventManager();
        }
        return this._instance as ClientEventManager;
    }
    private static _instance:ClientEventManager;
    //---------------instance end----------------------------------------------------


    eventDispatcher = new Laya.EventDispatcher();

    //派发事件
    public Event = function (eventId, data?: any) {
        this.eventDispatcher.event(eventId, data);
    }

    //监听事件
    public On = function (eventId, caller, listener, data?: any) {
        this.eventDispatcher.on(eventId, caller, listener, data);
    }

    //监听事件,事件响应一次后自动移除
    public Once = function (eventId, caller, listener, data?: any) {
        this.eventDispatcher.once(eventId, caller, listener, data);
    }

    //移除监听事件
    public Off = function (eventId, caller, listener, onceOnly = false) {
        this.eventDispatcher.off(eventId, caller, listener, onceOnly);
    }
}

class ClientEvent {
    static UPDATE_GAME_MONEY: string = "UPDATE_GAME_MONEY";// 更新游戏里的货币
}

 

你可能感兴趣的:(Laya)