You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
877 B
TypeScript
39 lines
877 B
TypeScript
|
3 years ago
|
import { LocalEmitter } from "./LocalEmitter";
|
||
|
|
|
||
|
|
const { ccclass, property } = cc._decorator;
|
||
|
|
|
||
|
|
|
||
|
|
@ccclass
|
||
|
|
export default class EmitterBase {
|
||
|
|
|
||
|
|
_comp_id_: number = LocalEmitter.newCompId();
|
||
|
|
|
||
|
|
getCompId() {
|
||
|
|
return this._comp_id_;
|
||
|
|
}
|
||
|
|
|
||
|
|
//注册监听
|
||
|
|
on(event: string, fn: Function) {
|
||
|
|
LocalEmitter.inst.on(this.getCompId(), event, fn);
|
||
|
|
}
|
||
|
|
|
||
|
|
//移除监听
|
||
|
|
off(event: string = null, fn: Function = null) {
|
||
|
|
LocalEmitter.inst.off(this.getCompId(), event, fn);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 发送事件
|
||
|
|
emit(event: string = null, obj: any = null) {
|
||
|
|
LocalEmitter.inst.emit(event, obj);
|
||
|
|
}
|
||
|
|
|
||
|
|
removeAllListeners() {
|
||
|
|
LocalEmitter.inst.off(this.getCompId());
|
||
|
|
}
|
||
|
|
|
||
|
|
initListener(config:{[key:string]:number}){
|
||
|
|
for(let key in config){
|
||
|
|
this.on(config[key].toString(), this[key].bind(this));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|