egret工具:事件监听管理器

时间:2018-04-22 09:48:06
【文件属性】:
文件名称:egret工具:事件监听管理器
文件大小:6KB
文件格式:ZIP
更新时间:2018-04-22 09:48:06
typescript egret html5 事件 src.zip中包含:DelegateUtil.ts ,Dictionary.ts ,BC.ts,Main.ts DelegateUtil.ts 函数代理,方便传递参数和函数执行空间地址,包含原始参数和附加参数。 Dictionary.ts 支持存取少量的“对象”来当作存储数据的key. 重点见介绍下面的类,养成习惯模块销毁时用一句 BC.removeEvent(this);可以有效防止出现遗漏移除的监听. 还有BC.addOnceEvent(...);方法是也是懒人必不可少的方法。 BC类在actionscript中已经用了将近8年了,版本陆陆续续改过5次,这几天根据ts的特性修改了一个版本,值得推荐给大家。 BC.ts : 支持模糊移除事件,“BC.removeEvent(this, ”后面3个参数为模糊参数,可有可无,明确的参数必须相等才会移除. * 000 删除所有关于监听者的所有事件,通常在类销毁时使用一次 BC.removeEvent(this); * 001 指定相同回调函数的所有监听 BC.removeEvent(this,null,null,func); * 010 指定事件名的所有监听 BC.removeEvent(this,null,Event.COMPLETE,null); * 011 指定事件名,指定回调函数的所有监听 BC.removeEvent(this,null,Event.COMPLETE,func); * 100 删除指定通知者 和 监听者之间的所有监听 BC.removeEvent(this,dispatch,null,null); * 101 删除通知者 和 监听者之间使用同一回调函数的所有监听 BC.removeEvent(this,dispatch,null,func); * 110 删除通知者 和 监听者之间指定事件的所有监听 BC.removeEvent(this,dispatch,Event.COMPLETE,null); * 111 明确删除指定的事件监听 BC.removeEvent(this,dispatch,Event.COMPLETE,func); 代码: class Main extends egret.DisplayObjectContainer{ //申明一个广播对象“dispatchSprite” private dispatchSprite: egret.Sprite; public constructor() { super(); //字典使用对象作为key引用存储数据.对象作为key实际上需要进行遍历索引,所以在同一个字典中尽量不要添加过多的key会影响性能. var dic: Dictionary = new Dictionary(); var arr1: string[] = ["我是数组"]; var obj2: any = { name: "我是对象" }; var str3: string = "我是字符"; //添加到字典 dic.add(arr1, arr1); dic.add(obj2, obj2); dic.add(str3, str3); //打印字典内部的数据 dic.dump(); //申明一个广播对象“dispatchSprite” this.dispatchSprite = new egret.Sprite(); //添加一个自动会移除监听事件 BC.addOnceEvent(this, this.dispatchSprite, egret.Event.ENTER_FRAME, this.onEnterFrameOnce); //创建和监听一个Timer事件 var timer: egret.Timer = new egret.Timer(50); BC.addEvent(this, timer, egret.TimerEvent.TIMER, this.onEnterTimer); timer.start(); } /** * dispatchSprite的帧事件 */ public onEnterFrame(event:egret.Event, index:string) { console.log("我是函数:", index); } /** * 只执行一次的dispatchSprite的帧事件 */ public onEnterFrameOnce(event:egret.Event) { console.log("我只执行了一次."); //生成3个代理函数的监听 for (var i: number = 0; i < 3; i++) { BC.addEvent(this, this.dispatchSprite, egret.Event.ENTER_FRAME, DelegateUtil.create(this, this.onEnterFrame, i+"")); } setTimeout(() => { //移除3个匿名代理函数 BC.removeEvent(this, this.dispatchSprite, egret.Event.ENTER_FRAME); console.log("3个匿名代理函数被移除事件了."); //新增加一个事件,100秒后一起移除. BC.addEvent(this, this.dispatchSprite, egret.Event.ENTER_FRAME, DelegateUtil.create(this, this.onEnterFrame, "新来的!")); setTimeout(() => { //移除所有该类的监听 BC.removeEvent(this); console.log("所有该类的监听移除了."); }, 100); }, 200); } /** * timer事件 */ public onEnterTimer(event: egret.TimerEvent) { console.log("我是Timer的事件."); } } 输出结果: key: 我是字符 ---> data: 我是字符 Dictionary.ts:205 key: object ---> data: ["我是数组"] Dictionary.ts:205 key: object ---> data: Object {name: "我是对象"} Main.ts:38我只执行了一次. Main.ts:62我是Timer的事件. Main.ts:31 我是函数: 0 Main.ts:31 我是函数: 1 Main.ts:31 我是函数: 2 Main.ts:31 我是函数: 0 Main.ts:31 我是函数: 1 Main.ts:31 我是函数: 2 Main.ts:31 我是函数: 0 Main.ts:31 我是函数: 1 Main.ts:31 我是函数: 2 Main.ts:62 我是Timer的事件. Main.ts:31 我是函数: 0 Main.ts:31 我是函数: 1 Main.ts:31 我是函数: 2 Main.ts:31 我是函数: 0 Main.ts:31 我是函数: 1 Main.ts:31 我是函数: 2 Main.ts:62 我是Timer的事件. Main.ts:31 我是函数: 0 Main.ts:31 我是函数: 1 Main.ts:31 我是函数: 2 Main.ts:31 我是函数: 0 Main.ts:31 我是函数: 1 Main.ts:31 我是函数: 2 Main.ts:62 我是Timer的事件. Main.ts:47 3个匿名代理函数被移除事件了. Main.ts:31 我是函数: 新来的! Main.ts:62 我是Timer的事件. Main.ts:31 我是函数: 新来的! Main.ts:62 我是Timer的事件. Main.ts:31 我是函数: 新来的! Main.ts:54 所有该类的监听移除了.
【文件预览】:
DelegateUtil.ts
BC.ts
Main.ts
Dictionary.ts

网友评论