CoinBord.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import Constant from "../Framework/Constant";
  2. import { cocosz } from "../Framework/CocosZ";
  3. import Msg from "../Framework/Msg";
  4. // @ts-ignore
  5. const i18n = require('LanguageData');
  6. const { ccclass, property } = cc._decorator;
  7. @ccclass
  8. export default class CoinBord extends cc.Component {
  9. private _label: cc.Label = null;
  10. private _icon: cc.Node = null;
  11. private _adBtn: cc.Node = null;
  12. @property
  13. isDiamond: boolean = false;
  14. onLoad() {
  15. this._label = this.node.getChildByName("Label").getComponent(cc.Label);
  16. this._icon = this.node.getChildByName("Icon");
  17. this._adBtn = this.node.getChildByName("BtnAD");
  18. if (this._adBtn) {
  19. this._adBtn.on(cc.Node.EventType.TOUCH_END, (() => {
  20. let node = cc.instantiate(cocosz.resMgr.getRes("UIADPanel", cc.Prefab));
  21. cc.find("Canvas").addChild(node);
  22. if (this.isDiamond) {
  23. node.getComponent("UIADPanel").setDiamond();
  24. }
  25. }));
  26. this._adBtn.active = cocosz.isADON;
  27. }
  28. }
  29. onEnable() {
  30. cc.game.on(Constant.E_GAME_LOGIC, this._onGameMessageHandler, this);
  31. this._updateLabel();
  32. }
  33. onDisable() {
  34. cc.game.targetOff(this);
  35. }
  36. private _onGameMessageHandler(event: any) {
  37. switch (event.type) {
  38. case Constant.E_COIN_CHANGE: {
  39. this._updateLabel();
  40. break;
  41. }
  42. case Constant.E_Diamond_CHANGE: {
  43. this._updateLabel();
  44. break;
  45. }
  46. }
  47. }
  48. private _updateLabel() {
  49. if (this.isDiamond) {
  50. this._label.string = cocosz.dataMgr.DiamondCount + "";
  51. }
  52. else {
  53. this._label.string = cocosz.dataMgr.CoinCount + "";
  54. }
  55. }
  56. /**
  57. * 获取Icon的世界坐标位置(金币动画飞入使用)
  58. */
  59. public getLocation() {
  60. let pos = this._icon.convertToWorldSpaceAR(cc.Vec3.ZERO);
  61. // let temp = cc.v2(pos.x, cc.winSize.height - 76)
  62. return pos;
  63. }
  64. private _addGold() {
  65. cocosz.dataMgr.CoinCount += 200;
  66. Msg.Show(i18n.t("msg.gxhdjb") + "200");//恭喜获得金币
  67. }
  68. }