StarHandbook.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. const StarApi = require('../net/StarApi');
  2. const GameModule = require('../utils/GameModule');
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. //
  7. starItemPrefab: cc.Prefab,
  8. starLayout: cc.Layout,
  9. //
  10. progressBar: cc.ProgressBar,
  11. countLabel: cc.Label,
  12. // percentLabel: cc.Label,
  13. percentRichText: cc.RichText,
  14. //
  15. descNode: cc.Node,
  16. },
  17. // LIFE-CYCLE CALLBACKS:
  18. onLoad () {
  19. GameEvent.on('show_star_desc', this, (starInfo) => {
  20. this.showDescView(starInfo);
  21. });
  22. },
  23. onDestroy() {
  24. GameModule.audioMng.playClickButton();
  25. GameEvent.off('show_star_desc', this);
  26. },
  27. start () {
  28. this.getAllStars().then((respondData) => {
  29. console.log(respondData);
  30. this.configData(respondData);
  31. }).catch((code) => {
  32. console.log(code);
  33. });
  34. },
  35. /// 网络请求
  36. getAllStars() {
  37. return new Promise((resolve, reject) => {
  38. // 获取目标用户的建筑
  39. StarApi.getAllStars((respondData) => {
  40. resolve(respondData);
  41. }, (code) => {
  42. reject(code);
  43. });
  44. })
  45. },
  46. configData(respondData) {
  47. this.respondData = respondData;
  48. if (respondData.userStars.length > 0) {
  49. var isUnlockedCount = 0;
  50. for (let i = 0; i < respondData.userStars.length; ++i) {
  51. let star = respondData.userStars[i];
  52. let item = cc.instantiate(this.starItemPrefab);
  53. item = item.getComponent('StarHandbookItem');
  54. item.configData(star);
  55. this.starLayout.node.addChild(item.node);
  56. if (star.starCount > 0) {
  57. isUnlockedCount++;
  58. }
  59. }
  60. let totalCount = respondData.userStars.length;
  61. this.countLabel.string = `${isUnlockedCount}/${totalCount} 已收集的明星`;
  62. let percent = (isUnlockedCount/totalCount * 100).toFixed(2);
  63. // this.percentLabel.string = `${percent}%`;
  64. this.percentRichText.string = `<b><outline color=#3d2e1d width=2><color=#FFD800 >${percent}%</color></outline></b>`;
  65. this.progressBar.progress = isUnlockedCount/totalCount;
  66. }
  67. },
  68. closeView() {
  69. this.node.destroy();
  70. },
  71. //显示描述信息
  72. showDescView(starInfo) {
  73. GameModule.audioMng.playClickButton();
  74. this.descNode.active = true;
  75. this.descNode.getComponent('StarHandbookDesc').configData(starInfo);
  76. },
  77. closeDescView() {
  78. GameModule.audioMng.playClickButton();
  79. this.descNode.active = false;
  80. },
  81. // update (dt) {},
  82. });