LayaBox 自定义事件

EventMgrts.ts文件

import EventDispatcher = laya.events.EventDispatcher;
class EventMgr extends EventDispatcher {
    static eventDispatcher: EventDispatcher = new EventDispatcher();
    static _instance: EventMgr;
    public static getInstance() {
        if (EventMgr._instance == null) {
            EventMgr._instance = new EventMgr();
        }
        return EventMgr._instance;
    }
    constructor() {
        super();
    }
    ///注册事件
    public Emit(InName, agv?: null) {
        //派发事件
        console.log("派发事件",InName);
        EventMgr.eventDispatcher.event(InName, agv);
    }
    //侦听事件
    public AddNotice(InName, caller, listener: Function, arg?: any[]): void {
        console.log("侦听事件",InName);
        EventMgr.eventDispatcher.on(InName, caller, listener, (arg == null) ? null : ([arg]));
    }
}

添加侦听事件

EventMgr.getInstance().AddNotice("test", this, this.onAddNotice);

注册事件

 EventMgr.getInstance().Emit("test");
    原文作者:深海不蓝
    原文地址: https://segmentfault.com/a/1190000016970840
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞