DrawScroll.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.scrollNode.height = 1020;
  53. }
  54. this.node.active = true;
  55. },
  56. initData() {
  57. this.iconCount = 5;
  58. this.upMoveSpeed = 35;
  59. this.upMoveTotal = 0;
  60. this.upMoveMax = 500;
  61. this.downMoveSpeed = 35;
  62. this.downMoveTotal = 0;
  63. // this.downScroolMax = 0;
  64. },
  65. downScroll() {
  66. /// 降速到最后 该减速了
  67. if (this.downMoveSpeed == 35 && this.downMoveTotal > this.downScroolMax / 2) {
  68. this.downMoveSpeed = 25;
  69. } else if (this.downMoveSpeed == 25 && this.downMoveTotal > this.downScroolMax * 3 / 4) {
  70. this.downMoveSpeed = 20;
  71. } else if (this.downMoveSpeed == 20 && this.downMoveTotal > this.downScroolMax * 4 / 5) {
  72. this.downMoveSpeed = 15;
  73. } else if (this.downMoveSpeed == 15 && this.downMoveTotal > this.downScroolMax) {
  74. this.downMoveSpeed = 5;
  75. }
  76. let tempSpeed = this.downMoveSpeed;
  77. let length = this.StarNodeArr.length;
  78. for (let i = 0; i < length; i ++) {
  79. let childNode = this.StarNodeArr[i];
  80. let childY = childNode.y - this.downMoveSpeed;
  81. /// 如果最后一个都开始移动啦
  82. if (childY < this.lastNodeY ) {
  83. /// 在最上面对应的node 7 -> 0 6 -> 7
  84. let count = i == (length - 1) ? 0 : (i + 1);
  85. let starNode = this.StarNodeArr[count];
  86. /// 如果小于说明已经移动了,直接加上10 + 208就行了
  87. if (count < i) {
  88. childNode.y = starNode.y + 10 + 208;
  89. } else {
  90. childNode.y = starNode.y + 10 + 208 - this.downMoveSpeed;
  91. }
  92. } else {
  93. childNode.y = childY;
  94. if (this.downMoveSpeed <= 10) {
  95. if (childY > 0 && childY < this.downMoveSpeed) {
  96. tempSpeed = this.downMoveSpeed - childY;
  97. ////滚到了抽奖的正确位置
  98. } else if (childY == 0) {
  99. tempSpeed = 0;
  100. this.scrollToIndex = i;
  101. let count = (i + 1) % 5;
  102. AlertManager.showDrawSuccessAlert(this.drawData, this.typeId, count);
  103. }
  104. }
  105. }
  106. }
  107. this.downMoveSpeed = tempSpeed;
  108. this.downMoveTotal += this.downMoveSpeed;
  109. },
  110. upScroll() {
  111. let length = this.StarNodeArr.length;
  112. for (let i = 0; i < length; i ++) {
  113. let childNode = this.StarNodeArr[i];
  114. let childY = childNode.y + this.upMoveSpeed;
  115. /// 如果第一个都开始移动啦
  116. if (childY > this.firstNodeY) {
  117. /// 在最上面对应的node 0 -> 7 6 -> 7
  118. let count = i == 0 ? (length - 1) : (i - 1);
  119. let starNode = this.StarNodeArr[count];
  120. if (count < i) {
  121. childNode.y = starNode.y - 10 - 208;
  122. } else {
  123. childNode.y = starNode.y - 10 - 208 + this.downMoveSpeed;
  124. }
  125. } else {
  126. childNode.y = childY;
  127. }
  128. }
  129. this.upMoveTotal += this.upMoveSpeed;
  130. },
  131. setUpNotification() {
  132. GameEvent.on("draw_done_action",this, () => {
  133. this.node.destroy();
  134. });
  135. GameEvent.on("draw_again", this,(againDrawSuccessData) => {
  136. this.drawData.propId = againDrawSuccessData.propId;
  137. this.drawData.propName = againDrawSuccessData.propName;
  138. /// 让它重新滚动
  139. this.initData();
  140. });
  141. },
  142. setUpStar() {
  143. let scroolNodeHeight = this.scrollNode.height;
  144. let starHeight = 208;
  145. let space = 10;
  146. /// 显示的个数
  147. let visibleCount = 0;
  148. let temp = scroolNodeHeight;
  149. while(temp > 0) {
  150. let updateLen = visibleCount == 0 ? starHeight / 2 : (10 + starHeight);
  151. temp -= updateLen;
  152. visibleCount ++;
  153. };
  154. visibleCount = visibleCount * 2 - 1;
  155. let arrCount = visibleCount + 2;
  156. this.StarNodeArr = Array(arrCount);
  157. this.centerCount = Math.floor(arrCount / 2);
  158. /// 滚动完一圈的长度
  159. let scrollCycleLength = (visibleCount - 1) * (starHeight + 10);
  160. this.downScroolMax = scrollCycleLength * 2;
  161. /// 多创建两个 用来放在最后和最前面
  162. DWTool.loadResPrefab("./prefabs/draw/drawStarContent")
  163. .then((result) => {
  164. for (let index = 0; index < arrCount; ++index) {
  165. let item = cc.instantiate(result);
  166. let y = (this.centerCount - index) * (space + starHeight);
  167. if (index == arrCount - 1) {
  168. this.lastNodeY = y;
  169. }
  170. if (index == 0) {
  171. this.firstNodeY = y;
  172. }
  173. item.setPosition(0, y);
  174. this.StarNodeArr[index] = item;
  175. this.scrollNode.addChild(item);
  176. let count = (index + 1) % 5;
  177. let script = item.getComponent('DrawStarContent');
  178. script.init(count);
  179. };
  180. });
  181. },
  182. });