AreaCode.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. itemTemplate: { // item template to instantiate other items
  5. default: null,
  6. type: cc.Prefab
  7. },
  8. scrollView: {
  9. default: null,
  10. type: cc.ScrollView
  11. },
  12. spawnCount: 0, // how many items we actually spawn
  13. totalCount: 0, // how many items we need for the whole list
  14. spacing: 0, // space between each item
  15. bufferZone: 0, // when item is away from bufferZone, we relocate it
  16. },
  17. // use this for initialization
  18. onLoad: function () {
  19. this.index = 0;
  20. this.content = this.scrollView.content;
  21. this.items = []; // array to store spawned items
  22. this.initialize();
  23. this.updateTimer = 0;
  24. this.updateInterval = 0.2;
  25. this.lastContentPosY = 0; // use this variable to detect if we are scrolling up or down
  26. },
  27. initialize: function () {
  28. this.height = this.itemTemplate.data.height;
  29. this.content.height = this.totalCount * (this.height + this.spacing) + this.spacing; // get total content height
  30. for (let i = 0; i < this.spawnCount; ++i) { // spawn items, we only need to do this once
  31. let item = cc.instantiate(this.itemTemplate);
  32. item.parent = this.content;
  33. item.setPosition(0, -item.height * (0.5 + i) - this.spacing * (i + 1));
  34. this.items.push(item);
  35. item.getChildByName('CodeLabel').getComponent(cc.Label).string = this.index;
  36. this.index++;
  37. }
  38. },
  39. shutDown()
  40. {
  41. this.node.active = false
  42. },
  43. getPositionInView: function (item) { // get item position in scrollview's node space
  44. let worldPos = item.parent.convertToWorldSpaceAR(item.position);
  45. let viewPos = this.scrollView.node.convertToNodeSpaceAR(worldPos);
  46. return viewPos;
  47. },
  48. update: function(dt) {
  49. this.updateTimer += dt;
  50. if (this.updateTimer < this.updateInterval) return; // we don't need to do the math every frame
  51. this.updateTimer = 0;
  52. let items = this.items;
  53. let buffer = this.bufferZone;
  54. let isDown = this.scrollView.content.y < this.lastContentPosY; // scrolling direction
  55. let offset = (this.height + this.spacing) * items.length;
  56. for (let i = 0; i < items.length; ++i) {
  57. let viewPos = this.getPositionInView(items[i]);
  58. if (isDown) {
  59. // if away from buffer zone and not reaching top of content
  60. if (viewPos.y < -buffer && items[i].y + offset < 0) {
  61. items[i].y = items[i].y + offset;
  62. let item = items[i].getComponent('Item');
  63. // let itemId = item.itemID - items.length; // update item id
  64. // item.updateItem(itemId);
  65. }
  66. } else {
  67. // if away from buffer zone and not reaching bottom of content
  68. if (viewPos.y > buffer && items[i].y - offset > -this.content.height) {
  69. items[i].y = items[i].y - offset;
  70. let item = items[i].getComponent('Item');
  71. // let itemId = item.itemID + items.length;
  72. // item.updateItem(itemId);
  73. }
  74. }
  75. }
  76. // update lastContentPosY
  77. this.lastContentPosY = this.scrollView.content.y;
  78. // this.lblTotalItems.textKey = "Total Items: " + this.totalCount;
  79. },
  80. // scrollEvent: function(sender, event) {
  81. // switch(event) {
  82. // case 0:
  83. // this.lblScrollEvent.string = "Scroll to Top";
  84. // break;
  85. // case 1:
  86. // this.lblScrollEvent.string = "Scroll to Bottom";
  87. // break;
  88. // case 2:
  89. // this.lblScrollEvent.string = "Scroll to Left";
  90. // break;
  91. // case 3:
  92. // this.lblScrollEvent.string = "Scroll to Right";
  93. // break;
  94. // case 4:
  95. // this.lblScrollEvent.string = "Scrolling";
  96. // break;
  97. // case 5:
  98. // this.lblScrollEvent.string = "Bounce Top";
  99. // break;
  100. // case 6:
  101. // this.lblScrollEvent.string = "Bounce bottom";
  102. // break;
  103. // case 7:
  104. // this.lblScrollEvent.string = "Bounce left";
  105. // break;
  106. // case 8:
  107. // this.lblScrollEvent.string = "Bounce right";
  108. // break;
  109. // case 9:
  110. // this.lblScrollEvent.string = "Auto scroll ended";
  111. // break;
  112. // }
  113. // },
  114. addItem: function() {
  115. this.content.height = (this.totalCount + 1) * (this.height + this.spacing) + this.spacing; // get total content height
  116. this.totalCount = this.totalCount + 1;
  117. },
  118. removeItem: function() {
  119. if (this.totalCount - 1 < 30) {
  120. cc.error("can't remove item less than 30!");
  121. return;
  122. }
  123. this.content.height = (this.totalCount - 1) * (this.height + this.spacing) + this.spacing; // get total content height
  124. this.totalCount = this.totalCount - 1;
  125. this.moveBottomItemToTop();
  126. },
  127. moveBottomItemToTop () {
  128. let offset = (this.height + this.spacing) * this.items.length;
  129. let length = this.items.length;
  130. let item = this.getItemAtBottom();
  131. // whether need to move to top
  132. if (item.y + offset < 0) {
  133. item.y = item.y + offset;
  134. let itemComp = item.getComponent('Item');
  135. // let itemId = itemComp.itemID - length;
  136. // itemComp.updateItem(itemId);
  137. }
  138. },
  139. getItemAtBottom () {
  140. let item = this.items[0];
  141. for (let i = 1; i < this.items.length; ++i) {
  142. if (item.y > this.items[i].y) {
  143. item = this.items[i];
  144. }
  145. }
  146. return item;
  147. },
  148. scrollToFixedPosition: function () {
  149. this.scrollView.scrollToOffset(cc.v2(0, 500), 2);
  150. }
  151. });