gameToast.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. export default class gameToast {
  2. private static instance: gameToast = null;
  3. private showObj: cc.Node = null;
  4. private scheduleObj = null;
  5. private callback = null;
  6. private constructor() { }
  7. public static getInstance(): gameToast {
  8. if (gameToast.instance === null) {
  9. gameToast.instance = new gameToast();
  10. }
  11. return gameToast.instance;
  12. }
  13. /**
  14. * 显示toast
  15. * @param content 显示的内容
  16. * @param time 清除的时间
  17. * @param callback 清除时候回调
  18. */
  19. public show(parent:cc.Node, content: string, time: number, callback: Function, target:cc.Component) {
  20. this.callback = callback;
  21. if(this.showObj){
  22. this.showObj.parent = parent;
  23. let DetailLabel = this.showObj.getChildByName('DetailLabel');
  24. DetailLabel.getComponent(cc.Label).string = content;
  25. this.scheduleObj = null;
  26. target.unschedule(this.scheduleObj);
  27. this.scheduleObj = target.scheduleOnce(() => {
  28. if (callback) {
  29. callback();
  30. this.callback = null;
  31. }
  32. this.showObj.destroy();
  33. this.showObj = null;
  34. }, time);
  35. }else{
  36. cc.loader.loadRes("prefab/gameToast", function (err, texture) {
  37. this.showObj = cc.instantiate(texture);
  38. this.showObj.parent = parent;
  39. let DetailLabel = this.showObj.getChildByName('DetailLabel');
  40. DetailLabel.getComponent(cc.Label).string = content;
  41. this.scheduleObj = target.scheduleOnce(() => {
  42. if (callback) {
  43. callback();
  44. this.callback = null;
  45. }
  46. this.showObj.destroy();
  47. this.showObj = null;
  48. }, time);
  49. }.bind(this));
  50. }
  51. }
  52. public hide(target:cc.Component) {
  53. if (this.showObj) {
  54. if(this.callback){
  55. this.callback();
  56. }
  57. this.scheduleObj = null;
  58. target.unschedule(this.scheduleObj);
  59. }
  60. this.callback = null;
  61. this.showObj.destroy();
  62. this.showObj = null;
  63. }
  64. }