LayaBox--事件分发器
作者:互联网
/**
* 事件分发器
*/
namespace game.common {
export class EventManager extends Laya.EventDispatcher {
private static _ins: EventManager;
public static get ins(): EventManager {
if (!EventManager._ins) {
EventManager._ins = new EventManager();
}
return this._ins;
}
constructor() {
super();
}
public init(): void {
}
event(type: string, data?: any): boolean {
super.on;
return super.event(type, new NoticeEvent(type, data));
}
}
export class NoticeEvent {
public data: any;
public type: string;
constructor(type: string, data: any) {
this.type = type;
this.data = data;
}
/**资源加载完成 */
public static GAME_RES_LOAD_FINISH: string = "GAME_RES_LOAD_FINISH";
}
}
使用方法
//首先注册
EventManager.ins.on(CustomDefine.SCORE_CHANGE,this,this.updateScore);
public updateScore(event:game.common.NoticeEvent){
let score:number = event.data;
console.log(score);
}
//监听
public addScore(num:number){
this.score+=num;
game.common.EventManager.ins.event(CustomDefine.SCORE_CHANGE,this.score)
}
//触发
Laya.timer.loop(2000,this,()=>{
this.addScore(20);
});
标签:event,分发,type,ins,EventManager,LayaBox,data,public,事件 来源: https://blog.csdn.net/Yuanbo_Z/article/details/100115761