LevelHomeListAdapter.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. const ListAdapter = require('../utils/ListViewAdapter');
  2. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  3. const GameModule = require("../utils/GameModule");
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. scrollView: cc.ScrollView,
  8. content: cc.Node,
  9. item: cc.Prefab,
  10. itemScriptName: 'LevelHomeItem',
  11. //
  12. topItem: cc.Prefab,
  13. bottomItem: cc.Prefab,
  14. },
  15. // LIFE-CYCLE CALLBACKS:
  16. onLoad () {
  17. this.isMoving = false;
  18. this.listAdapter = new ListAdapter(this.scrollView);
  19. this.listAdapter.shouldRemove = false;
  20. this._isTouch = false;
  21. this.setupEvent();
  22. },
  23. setupEvent() {
  24. this.scrollView.node.on(cc.Node.EventType.TOUCH_START, (event) => {
  25. //// 如果有购买商品 一秒钟点击十次
  26. if (!this._isTouch) {
  27. if (Global.isLongPressClick === true) {
  28. /// 最起码要按住1秒
  29. this._isTouch = true;
  30. this._addClick = false;
  31. this._timeCount = 0;
  32. this.schedule(this.timeAction, 1);
  33. }
  34. }
  35. });
  36. this.scrollView.node.on(cc.Node.EventType.TOUCH_END, (event) => {
  37. /// 防止多点点击
  38. if (this._isTouch) {
  39. if (Global.isLongPressClick === true && this._addClick) {
  40. let clickCount = 1 / GameModule.userInfo.secondClick;
  41. clickCount -= 10;
  42. GameModule.userInfo.secondClick = 1 / clickCount;
  43. this._addClick = false;
  44. } else {
  45. this._addClick = true;
  46. }
  47. this._isTouch = false;
  48. this.unschedule(this.timeAction, this);
  49. }
  50. if (this.isMoving) {
  51. this.isMoving = false;
  52. return;
  53. }
  54. this.clickAddMoney();
  55. }, this);
  56. this.scrollView.node.on(cc.Node.EventType.TOUCH_MOVE, (event) => {
  57. let x = event.getLocationX();
  58. let y = event.getLocationY();
  59. let startLocation = event.getStartLocation();
  60. if (Math.abs(x - startLocation.x) > 5 || Math.abs(y - startLocation.y) > 5) {
  61. this.isMoving = true;
  62. }
  63. }, this);
  64. GameEvent.on(GameNotificationKey.StarEnterRoom, this, (starId, roomId) => {
  65. this.refreshStarEnterRoom(starId, roomId);
  66. });
  67. GameModule.homeGuide.on('Fire_state24', () => {
  68. this.scrollView.vertical = true;
  69. }, this);
  70. },
  71. clickAddMoney() {
  72. GameEvent.fire(GameNotificationKey.ClickAddMoney);
  73. },
  74. timeAction() {
  75. this._timeCount += 1;
  76. if (this._timeCount == 1) {
  77. if (!this._addClick) {
  78. let clickCount = 0;
  79. if (GameModule.userInfo.secondClick == 0) {
  80. clickCount = 10;
  81. } else {
  82. clickCount = 1 / GameModule.userInfo.secondClick;
  83. clickCount += 10;
  84. }
  85. GameModule.userInfo.secondClick = 1 / clickCount;
  86. this._addClick = true;
  87. }
  88. } else {
  89. GameModule.userInfo.clickCount += 10;
  90. }
  91. },
  92. start () {
  93. },
  94. configBuildings(dataList, isUnlock = false) {
  95. this.dataList = dataList;
  96. if (isUnlock) {
  97. this.listAdapter.addItemsToDataList(this.dataList[0],true);
  98. //上面的方法会重新计算cotent总高度,所以需要再加回底部的高度
  99. this.bottomItemNode.setPosition(0, -(this.bottomItemHeight * 0.5 + this.content.height));
  100. this.content.height += this.bottomItemHeight;
  101. if (GameModule.homeGuide.getComponent('HomeGuide').curState == 'state21' || GameModule.homeGuide.getComponent('HomeGuide').curState == 'state24') {
  102. this.scrollView.vertical = true;
  103. this.scrollView.scrollToBottom(0.0);
  104. setTimeout(() => {
  105. this.scrollView.vertical = false;
  106. }, 500);
  107. } else {
  108. this.scrollView.scrollToOffset(this.scrollPosition, 0.0);
  109. }
  110. } else {
  111. this.scrollView.content.removeAllChildren();
  112. let top = cc.instantiate(this.topItem);
  113. this.topItemHeight = top.height; // get total content height
  114. this.content.addChild(top);
  115. top.setPosition(0, -(this.topItemHeight * 0.5));
  116. let topItemScript = top.getComponent('LevelHomeTop');
  117. topItemScript.updateItem();
  118. this.listAdapter.updateItems(this.dataList, this.item, this.itemScriptName, this.topItemHeight);
  119. this.bottomItemNode = cc.instantiate(this.bottomItem);
  120. this.bottomItemHeight = this.bottomItemNode.height; // get total content height
  121. this.content.addChild(this.bottomItemNode);
  122. this.bottomItemNode.setPosition(0, -(this.bottomItemHeight * 0.5 + this.content.height));
  123. let itemScript = this.bottomItemNode.getComponent('LevelHomeBottom');
  124. itemScript.updateItem();
  125. //上面的方法会重新计算cotent总高度,所以需要再加回底部的高度
  126. this.bottomItemNode.setPosition(0, -(this.bottomItemHeight * 0.5 + this.content.height));
  127. this.content.height += this.bottomItemHeight;
  128. this.scrollView.scrollToBottom(0.1);
  129. this.scrollPosition = this.scrollView.getContentPosition();
  130. }
  131. },
  132. configGuide() {
  133. if (GameModule.homeGuide.getComponent('HomeGuide').curState == 'state20') {
  134. setTimeout(() => {
  135. this.scrollView.vertical = false;
  136. }, 500);
  137. return;
  138. }
  139. if (GameModule.homeGuide.getComponent('HomeGuide').curState == 'state24') {
  140. setTimeout(() => {
  141. this.scrollView.vertical = false;
  142. }, 500);
  143. return;
  144. }
  145. if (!GameModule.homeGuide.getComponent('HomeGuide').isPassGuideState('state24')) {
  146. setTimeout(() => {
  147. this.scrollView.vertical = false;
  148. }, 500);
  149. }
  150. },
  151. refreshRoomData(roomInfo) {
  152. this.dataList.forEach((item, index) => {
  153. if (item.roomId == roomInfo.roomId) {
  154. this.dataList[index] = roomInfo;
  155. }
  156. });
  157. },
  158. refreshStarEnterRoom(starId, roomId) {
  159. GameModule.userInfo.refreshSecondText();
  160. GameEvent.fire(GameNotificationKey.UpBuildingLevel);
  161. if (roomId <= 0) {
  162. return;
  163. }
  164. for (let i = 0; i < this.dataList.length; ++i) {
  165. let room = this.dataList[i];
  166. if (room.roomId == roomId) {
  167. if (room.roomStars == undefined) {
  168. room.roomStars = [];
  169. }
  170. room.roomStars.push(starId);
  171. }
  172. }
  173. this.listAdapter.updateDataList(this.dataList, roomId);
  174. },
  175. // update (dt) {},
  176. onScrollEvent(sender, event) {
  177. this.listAdapter.update();
  178. },
  179. });