UserPackItem.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. var {GameNotificationKey} = require('../utils/GameEnum');
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. backgroundNode: cc.Node,
  6. backgroundSelectedNode: cc.Node,
  7. itemNode: cc.Node,
  8. itemSprite: cc.Sprite,
  9. countLabel: cc.Label,
  10. isSelected: {
  11. get: function() {
  12. if (!this._isSelected) {
  13. this._isSelected = false;
  14. }
  15. return this._isSelected;
  16. },
  17. set: function(value) {
  18. this._isSelected = value;
  19. if (this._isSelected) {
  20. this.backgroundSelectedNode.active = true;
  21. this.backgroundNode.active = false;
  22. } else {
  23. this.backgroundSelectedNode.active = false;
  24. this.backgroundNode.active = true;
  25. }
  26. }
  27. },
  28. hasItem: {
  29. get: function() {
  30. if (!this._hasItem) {
  31. this._hasItem = false;
  32. }
  33. return this._hasItem;
  34. },
  35. set: function(value) {
  36. this._hasItem = value;
  37. if (this._hasItem) {
  38. this.itemNode.active = true;
  39. } else {
  40. this.itemNode.active = false;
  41. }
  42. }
  43. },
  44. },
  45. // LIFE-CYCLE CALLBACKS:
  46. onLoad () {
  47. this.winSize = cc.view.getVisibleSize();
  48. this.setEventListener();
  49. },
  50. setEventListener() {
  51. GameEvent.on(GameNotificationKey.RefreshPackItem, this, () => {
  52. this.isSelected = false;
  53. });
  54. },
  55. onDisable() {
  56. this.hasItem = false;
  57. this.isSelected = false;
  58. GameEvent.off(GameNotificationKey.RefreshPackItem, this);
  59. },
  60. init(index,page,itemInfo) {
  61. this.itemIndex = index;
  62. this.page = page;
  63. if (itemInfo != undefined) {
  64. this.hasItem = true;
  65. this.itemInfo = itemInfo;
  66. this.configData();
  67. } else {
  68. this.hasItem = false;
  69. }
  70. },
  71. configData() {
  72. let itemId = this.itemInfo.itemId;
  73. let count = this.itemInfo.count;
  74. cc.loader.loadRes('item/'+this.itemInfo.picId, cc.SpriteFrame, (err, spriteFrame) => {
  75. this.itemSprite.spriteFrame = spriteFrame;
  76. });
  77. this.countLabel.string = count;
  78. },
  79. start () {
  80. },
  81. // update (dt) {},
  82. selectItem() {
  83. if (this.hasItem == false) {
  84. return;
  85. }
  86. if (this.isSelected == true) {
  87. this.isSelected = false;
  88. GameEvent.fire(GameNotificationKey.HidePropDesc);
  89. } else {
  90. GameEvent.fire(GameNotificationKey.RefreshPackItem);
  91. this.isSelected = true;
  92. let position = this.getWorldPosition(this.node);
  93. GameEvent.fire(GameNotificationKey.ShowPropDesc,position, this.itemInfo);
  94. }
  95. },
  96. getWorldPosition(node) {
  97. return node.convertToNodeSpaceAR(cc.p((this.winSize.width/2), (this.winSize.height/2)));
  98. },
  99. });