export default class gameToast { private static instance: gameToast = null; private showObj: cc.Node = null; private scheduleObj = null; private callback = null; private constructor() { } public static getInstance(): gameToast { if (gameToast.instance === null) { gameToast.instance = new gameToast(); } return gameToast.instance; } /** * 显示toast * @param content 显示的内容 * @param time 清除的时间 * @param callback 清除时候回调 */ public show(parent:cc.Node, content: string, time: number, callback: Function, target:cc.Component) { this.callback = callback; if(this.showObj){ this.showObj.parent = parent; let DetailLabel = this.showObj.getChildByName('DetailLabel'); DetailLabel.getComponent(cc.Label).string = content; this.scheduleObj = null; target.unschedule(this.scheduleObj); this.scheduleObj = target.scheduleOnce(() => { if (callback) { callback(); this.callback = null; } this.showObj.destroy(); this.showObj = null; }, time); }else{ cc.loader.loadRes("prefab/gameToast", function (err, texture) { this.showObj = cc.instantiate(texture); this.showObj.parent = parent; let DetailLabel = this.showObj.getChildByName('DetailLabel'); DetailLabel.getComponent(cc.Label).string = content; this.scheduleObj = target.scheduleOnce(() => { if (callback) { callback(); this.callback = null; } this.showObj.destroy(); this.showObj = null; }, time); }.bind(this)); } } public hide(target:cc.Component) { if (this.showObj) { if(this.callback){ this.callback(); } this.scheduleObj = null; target.unschedule(this.scheduleObj); } this.callback = null; this.showObj.destroy(); this.showObj = null; } }