integralInfo.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * 积分兑换种子
  3. */
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. inputValue: {
  8. default: 1,
  9. visible: false
  10. },
  11. //输入节点
  12. inputContainer: {
  13. default: null,
  14. type: cc.Node,
  15. serializable: true
  16. },
  17. //1 对应第一个种子,2对应第二个种子,3对应第三个种子
  18. toggleInputValue: {
  19. default: '0',
  20. visible: false,
  21. serializable:false
  22. },
  23. seeds: {
  24. default: [],
  25. visible: false
  26. },
  27. seedsIcon: {
  28. default: [],
  29. type: [cc.Sprite]
  30. },
  31. buildingView: cc.Node,
  32. availableCNT: {
  33. default: null,
  34. type: cc.Label
  35. },
  36. consumeCNT: {
  37. default: null,
  38. type: cc.Label
  39. }
  40. },
  41. // LIFE-CYCLE CALLBACKS:
  42. onLoad() {
  43. this.buildingViewScript = this.buildingView.getComponent('BuildingView');
  44. //获取一个随机种子
  45. GlobalD.GameData.onGetPlayerSeeds((value) => {
  46. // console.log("onGetPlayerSeeds:", value);
  47. if (0 === value.code && value.flag) {
  48. this.availableCNT.string = value.data.score.oldTotalCnt + "/" + value.data.score.totalCount;
  49. this.seeds = value.data.seeds;
  50. for (let i = 0; i < 3; i++) {
  51. if (i >= this.seeds.length) return;
  52. switch (this.seeds[i].picture) {
  53. case 'Cabbage':
  54. this.seedsIcon[i].spriteFrame = this.buildingViewScript.seedIconSpriteFrame[0];
  55. break
  56. case 'Potato':
  57. this.seedsIcon[i].spriteFrame = this.buildingViewScript.seedIconSpriteFrame[1];
  58. break
  59. case 'Carrot':
  60. this.seedsIcon[i].spriteFrame = this.buildingViewScript.seedIconSpriteFrame[2];
  61. break
  62. case 'Broccoli':
  63. this.seedsIcon[i].spriteFrame = this.buildingViewScript.seedIconSpriteFrame[3];
  64. break
  65. case 'Tomato':
  66. this.seedsIcon[i].spriteFrame = this.buildingViewScript.seedIconSpriteFrame[4];
  67. break
  68. case 'Squash':
  69. this.seedsIcon[i].spriteFrame = this.buildingViewScript.seedIconSpriteFrame[5];
  70. break
  71. case 'Eggplant':
  72. this.seedsIcon[i].spriteFrame = this.buildingViewScript.seedIconSpriteFrame[6];
  73. break
  74. case 'Pepper':
  75. this.seedsIcon[i].spriteFrame = this.buildingViewScript.seedIconSpriteFrame[7];
  76. break
  77. case 'Lentil':
  78. this.seedsIcon[i].spriteFrame = this.buildingViewScript.seedIconSpriteFrame[8];
  79. break
  80. default:
  81. console.error("兑换种子信息错误?");
  82. break
  83. }
  84. }
  85. this._updatePrice();
  86. }
  87. })
  88. },
  89. // start() {
  90. // },
  91. onToggleInput(value, evnentData) {
  92. // console.log(value.isChecked, evnentData);
  93. this.toggleInputValue = evnentData;
  94. this._updatePrice();
  95. },
  96. onOpenInfo() {
  97. this.node.active = true;
  98. },
  99. onClose() {
  100. this.node.active = false;
  101. },
  102. inputEditValue(value, e) {
  103. var numberTemp = new RegExp("^[A-Za-z0-9]+$");
  104. let limitValue = 10;
  105. if (numberTemp.test(value)) {
  106. if (Number(value) >= 1 && Number(value) <= limitValue) {
  107. this.inputValue = Number(value);
  108. } else if (Number(value) > limitValue) {
  109. //限制只能输入100
  110. this.inputValue = limitValue;
  111. this.inputContainer.getComponent(cc.EditBox).string = this.inputValue;
  112. } else {
  113. this.inputValue = 1;
  114. this.inputContainer.getComponent(cc.EditBox).string = this.inputValue;
  115. }
  116. } else {
  117. this.inputValue = 1;
  118. this.inputContainer.getComponent(cc.EditBox).string = this.inputValue;
  119. // console.log("请输入整数的倍数", this.inputValue);
  120. }
  121. this._updatePrice();
  122. },
  123. _updatePrice() {
  124. let currtSeed = this.seeds[Number(this.toggleInputValue)];
  125. this.consumeCNT.string = '消耗积分:' + this.inputValue * currtSeed.priceCnt;
  126. },
  127. onExchangeSeed() {
  128. let data = { amount: this.inputValue, seedId: this.seeds[Number(this.toggleInputValue)].id }
  129. GlobalD.GameData.onGetPlayerScore(data, (value) => {
  130. // console.log("onGetPlayerScore:", value);
  131. if (0 === value.code && value.flag) {
  132. // this.seeds = value;
  133. this.availableCNT.string = value.data.score.oldTotalCnt + "/" + value.data.score.totalCount;
  134. //更新仓库
  135. this.buildingViewScript.onUpdateList();
  136. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), value.msg, 2);
  137. } else {
  138. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), value.msg, 2);
  139. }
  140. });
  141. }
  142. });