StoreCoin.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. },
  40. updateInitData() {
  41. this._content.getShopsByType(3).then( (respondData) => {
  42. this._shops = respondData.shops;
  43. for (let i = 0; i < this._items.length; ++ i) {
  44. let item = this._items[i].getComponent('StoreSmallItem');
  45. item._shopData = this._shops[i];
  46. item.updateTime();
  47. }
  48. }).catch((code, msg) => {
  49. console.log(code, msg);
  50. })
  51. },
  52. onDestroy() {
  53. GameEvent.off(GameNotificationKey.GameShowNotificationKey, this);
  54. },
  55. // update (dt) {},
  56. });