DrawScroll.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. const DWTool = require("../utils/DWTool");
  2. const AlertManager = require('../utils/AlertManager');
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. scrollNode: cc.Node,
  7. drawPointer: cc.Node,
  8. scrollBg: cc.Sprite,
  9. },
  10. // LIFE-CYCLE CALLBACKS:
  11. onLoad () {
  12. this.node.active = false;
  13. },
  14. update (dt) {
  15. if (this.scrollNode.children.length < this.iconCount + 2 ){
  16. return;
  17. }
  18. if (this.downMoveSpeed == 0) {
  19. return;
  20. }
  21. if (this.upMoveTotal < this.upMoveMax) {
  22. this.upScroll();
  23. } else {
  24. this.downScroll();
  25. }
  26. },
  27. onDestroy() {
  28. GameEvent.off("draw_done_action", this);
  29. GameEvent.off("draw_again", this);
  30. },
  31. init(drawData, typeId) {
  32. this.drawData = drawData;
  33. this.typeId = typeId;
  34. this.initData();
  35. this.setUpStar();
  36. this.setUpNotification();
  37. if (typeId > 1) {
  38. let path = './textures/draw/7000' + typeId;
  39. DWTool.loadResSpriteFrame(path).then((spriteFrame) => {
  40. this.scrollBg.spriteFrame = spriteFrame;
  41. }).catch((err) => {
  42. console.log(err);
  43. })
  44. this.scrollNode.x = 20;
  45. if (typeId == 2) {
  46. this.scrollBg.node.height = 1245;
  47. this.scrollBg.node.width = 353;
  48. } else {
  49. this.scrollBg.node.height = 1337;
  50. this.scrollBg.node.width = 408;
  51. }
  52. this.node.height = GameGlobal.winSize.height;
  53. if (GameGlobal.winSize.height <= 1000) {
  54. this.scrollNode.height = 800;
  55. this.scrollBg.node.height = 967;
  56. } else {
  57. this.scrollNode.height = 1020;
  58. }
  59. }
  60. this.node.active = true;
  61. },
  62. initData() {
  63. this.iconCount = 5;
  64. this.upMoveSpeed = 35;
  65. this.upMoveTotal = 0;
  66. this.upMoveMax = 500;
  67. this.downMoveSpeed = 35;
  68. this.downMoveTotal = 0;
  69. // this.downScroolMax = 0;
  70. },
  71. downScroll() {
  72. /// 降速到最后 该减速了
  73. if (this.downMoveSpeed == 35 && this.downMoveTotal > this.downScroolMax / 2) {
  74. this.downMoveSpeed = 25;
  75. } else if (this.downMoveSpeed == 25 && this.downMoveTotal > this.downScroolMax * 3 / 4) {
  76. this.downMoveSpeed = 20;
  77. } else if (this.downMoveSpeed == 20 && this.downMoveTotal > this.downScroolMax * 4 / 5) {
  78. this.downMoveSpeed = 15;
  79. } else if (this.downMoveSpeed == 15 && this.downMoveTotal > this.downScroolMax) {
  80. this.downMoveSpeed = 5;
  81. }
  82. let tempSpeed = this.downMoveSpeed;
  83. let length = this.StarNodeArr.length;
  84. for (let i = 0; i < length; i ++) {
  85. let childNode = this.StarNodeArr[i];
  86. let childY = childNode.y - this.downMoveSpeed;
  87. /// 如果最后一个都开始移动啦
  88. if (childY < this.lastNodeY ) {
  89. /// 在最上面对应的node 7 -> 0 6 -> 7
  90. let count = i == (length - 1) ? 0 : (i + 1);
  91. let starNode = this.StarNodeArr[count];
  92. /// 如果小于说明已经移动了,直接加上10 + 208就行了
  93. if (count < i) {
  94. childNode.y = starNode.y + 10 + 208;
  95. } else {
  96. childNode.y = starNode.y + 10 + 208 - this.downMoveSpeed;
  97. }
  98. } else {
  99. childNode.y = childY;
  100. if (this.downMoveSpeed <= 10) {
  101. if (childY > 0 && childY < this.downMoveSpeed) {
  102. tempSpeed = this.downMoveSpeed - childY;
  103. ////滚到了抽奖的正确位置
  104. } else if (childY == 0) {
  105. tempSpeed = 0;
  106. this.scrollToIndex = i;
  107. let count = (i + 1) % 5;
  108. AlertManager.showDrawSuccessAlert(this.drawData, this.typeId, count);
  109. }
  110. }
  111. }
  112. }
  113. this.downMoveSpeed = tempSpeed;
  114. this.downMoveTotal += this.downMoveSpeed;
  115. },
  116. upScroll() {
  117. let length = this.StarNodeArr.length;
  118. for (let i = 0; i < length; i ++) {
  119. let childNode = this.StarNodeArr[i];
  120. let childY = childNode.y + this.upMoveSpeed;
  121. /// 如果第一个都开始移动啦
  122. if (childY > this.firstNodeY) {
  123. /// 在最上面对应的node 0 -> 7 6 -> 7
  124. let count = i == 0 ? (length - 1) : (i - 1);
  125. let starNode = this.StarNodeArr[count];
  126. if (count < i) {
  127. childNode.y = starNode.y - 10 - 208;
  128. } else {
  129. childNode.y = starNode.y - 10 - 208 + this.downMoveSpeed;
  130. }
  131. } else {
  132. childNode.y = childY;
  133. }
  134. }
  135. this.upMoveTotal += this.upMoveSpeed;
  136. },
  137. setUpNotification() {
  138. GameEvent.on("draw_done_action",this, () => {
  139. this.node.destroy();
  140. });
  141. GameEvent.on("draw_again", this,(againDrawSuccessData) => {
  142. this.drawData.propId = againDrawSuccessData.propId;
  143. this.drawData.propName = againDrawSuccessData.propName;
  144. /// 让它重新滚动
  145. this.initData();
  146. });
  147. },
  148. setUpStar() {
  149. let scroolNodeHeight = this.scrollNode.height;
  150. let starHeight = 208;
  151. let space = 10;
  152. /// 显示的个数
  153. let visibleCount = 0;
  154. let temp = scroolNodeHeight;
  155. while(temp > 0) {
  156. let updateLen = visibleCount == 0 ? starHeight / 2 : (10 + starHeight);
  157. temp -= updateLen;
  158. visibleCount ++;
  159. };
  160. visibleCount = visibleCount * 2 - 1;
  161. let arrCount = visibleCount + 2;
  162. this.StarNodeArr = Array(arrCount);
  163. this.centerCount = Math.floor(arrCount / 2);
  164. /// 滚动完一圈的长度
  165. let scrollCycleLength = (visibleCount - 1) * (starHeight + 10);
  166. this.downScroolMax = scrollCycleLength * 1.5;
  167. /// 多创建两个 用来放在最后和最前面
  168. DWTool.loadResPrefab("./prefabs/draw/drawStarContent")
  169. .then((result) => {
  170. for (let index = 0; index < arrCount; ++index) {
  171. let item = cc.instantiate(result);
  172. let y = (this.centerCount - index) * (space + starHeight);
  173. if (index == arrCount - 1) {
  174. this.lastNodeY = y;
  175. }
  176. if (index == 0) {
  177. this.firstNodeY = y;
  178. }
  179. item.setPosition(0, y);
  180. this.StarNodeArr[index] = item;
  181. this.scrollNode.addChild(item);
  182. let count = (index + 1) % 5;
  183. let script = item.getComponent('DrawStarContent');
  184. script.init(count);
  185. };
  186. });
  187. },
  188. });