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");
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  •   JavaScript 与 HTML 之间的交互是通过事件实现的。   事件,就是文档或浏览器窗口中发生的一些特...
    霜天晓阅读 3,543评论 1 11
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,993评论 19 139
  • core package 概要:Core是所有其他包的基础包.它提供了大部分功能包括metadata,templa...
    LOVE小狼阅读 2,653评论 0 3
  • 事件流 JavaScript与HTML之间的交互是通过事件实现的。事件,就是文档或浏览器窗口中发生的一些特定的交互...
    DHFE阅读 848评论 0 3
  • 回来碰到床的时候就像被泄了气的气球,突然就瘫软在床上,人也是迷迷糊糊的打盹,双眼皮互相掐架了。 很多时候会发来信息...
    梦游世界阅读 233评论 0 0