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");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章