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.

97 lines
1.9 KiB
TypeScript

export interface IDisposer {
(): void
}
import { LocalEmitter } from "../external/LocalEmitter";
const { ccclass, property } = cc._decorator;
@ccclass
export default class BaseComp extends cc.Component {
_comp_id_: number = LocalEmitter.newCompId();
protected _pool: Array<IDisposer> = []
_frameCount = 0;
_frameLimit = 3;
canUpdate(){
this._frameCount++;
if(this._frameCount < this._frameLimit){
return false;
}
this._frameCount = 0;
return true;
}
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, obj2: any = null) {
LocalEmitter.inst.emit(event, obj, obj2);
}
initListener(config:{[key:string]:number}){
for(let key in config){
this.on(config[key].toString(), this[key].bind(this));
}
}
onDestroy() {
this.removeAllListeners();
}
removeAllListeners(){
LocalEmitter.inst.off(this.getCompId());
}
showPopWait(msg: string = "Vui lòng chờ..."): void {
LocalEmitter.inst.emit("showPopWait",{prompt:msg})
}
hidePopWait(): void {
LocalEmitter.inst.emit("hidePopWait")
}
mute(){
this._pool && this._pool.forEach((d) => d())
this._pool = [];
}
onDisable(){
this.mute();
}
onEnable(){
this.mute();
}
//加载图片资源
loadSpriteFrame(PrefabUrl, fun: Function): void {
cc.resources.load(PrefabUrl, cc.SpriteFrame, function (err, res) {
//检查资源加载
if (err) {
return
}
if (fun) {
fun(res)
}
})
}
}