StoreCoin.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Learn cc.Class:
  2. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. item: cc.Prefab,
  7. content: cc.Node
  8. },
  9. // LIFE-CYCLE CALLBACKS:
  10. // onLoad () {},
  11. start () {
  12. },
  13. init(content) {
  14. if (this._shops != undefined) {
  15. return;
  16. }
  17. this._content = content;
  18. this._shops = {};
  19. let contentWidth = this.content.width;
  20. let itemWith = Math.floor((contentWidth - 75) / 2);
  21. this._items = []
  22. content.getShopsByType(3).then( (respondData) => {
  23. this._shops = respondData.shops;
  24. for (let i = 0; i < this._shops.length; ++ i) {
  25. let coinItem = cc.instantiate(this.item);
  26. let coinItemWidth = coinItem.width;
  27. let coinItemHeight = coinItem.height;
  28. let radio = coinItemHeight / coinItemWidth;
  29. coinItem.width = itemWith;
  30. coinItem.height = coinItemHeight * radio;
  31. coinItem.getComponent('StoreSmallItem').init(this._shops[i], 3, i);
  32. this.content.addChild(coinItem);
  33. this._items.push(coinItem);
  34. }
  35. }).catch((code, msg) => {
  36. console.log(code, msg);
  37. })
  38. GameEvent.on(GameNotificationKey.GameShowNotificationKey, this, this.updateInitData);
  39. GameEvent.on('storeCoin_need_update', this, this.updateInitData);
  40. },
  41. updateInitData() {
  42. this._content.getShopsByType(3).then( (respondData) => {
  43. this._shops = respondData.shops;
  44. for (let i = 0; i < this._items.length; ++ i) {
  45. let item = this._items[i].getComponent('StoreSmallItem');
  46. item._shopData = this._shops[i];
  47. item.updateTime();
  48. }
  49. }).catch((code, msg) => {
  50. console.log(code, msg);
  51. })
  52. },
  53. onDestroy() {
  54. GameEvent.off(GameNotificationKey.GameShowNotificationKey, this);
  55. GameEvent.off('storeCoin_need_update', this);
  56. },
  57. // update (dt) {},
  58. });