RankScene.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Learn cc.Class:
  2. // - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
  4. // Learn Attribute:
  5. // - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. friendRankNode: {
  14. default: null,
  15. type: cc.Node
  16. },
  17. worldBtnDefault: {
  18. default: null,
  19. type: cc.SpriteFrame
  20. },
  21. worldBtnActive: {
  22. default: null,
  23. type: cc.SpriteFrame
  24. },
  25. friendBtnDefault: {
  26. default: null,
  27. type: cc.SpriteFrame
  28. },
  29. friendBtnActive: {
  30. default: null,
  31. type: cc.SpriteFrame
  32. },
  33. status: {
  34. get: function () {
  35. return this._status;
  36. },
  37. set: function (value) {
  38. this._status = value;
  39. if (this.friendBtn == null) return;
  40. if (value == 1) {
  41. this.friendBtn.getComponent(cc.Sprite).spriteFrame = this.friendBtnActive;
  42. this.worldBtn.getComponent(cc.Sprite).spriteFrame = this.worldBtnDefault;
  43. this.worldContentNode.active = false;
  44. this.friendContentNode.active = true;
  45. } else if (value == 2) {
  46. this.friendBtn.getComponent(cc.Sprite).spriteFrame = this.friendBtnDefault;
  47. this.worldBtn.getComponent(cc.Sprite).spriteFrame = this.worldBtnActive;
  48. this.worldContentNode.active = true;
  49. this.friendContentNode.active = false;
  50. }
  51. }
  52. },
  53. _viewitemTemp: {
  54. default: null
  55. },
  56. worldRankData: [],
  57. items: [],
  58. spacing: 10,
  59. spawnCount: 20,
  60. totalCount: 50,
  61. bufferZone: 800
  62. },
  63. // LIFE-CYCLE CALLBACKS:
  64. onLoad: function () {
  65. this.PHB = cc.find("Canvas/PHB")
  66. this.rankNode = this.PHB.getChildByName("Rank")
  67. this.worldContentNode = this.rankNode.getChildByName('WorldContent')
  68. this.friendContentNode = this.rankNode.getChildByName('FriendContent')
  69. this.worldScrollView = this.worldContentNode.getChildByName("rankScrollView")
  70. this.worldScrollViewComponent = this.worldScrollView.getComponent(cc.ScrollView)
  71. this.content = this.worldScrollViewComponent.content
  72. if (this._viewitemTemp == null || this._viewitemTemp == 'undefined') {
  73. this._viewitemTemp = this.content.children[0];
  74. this.content.removeChild(this._viewitemTemp);
  75. }
  76. this.navNode = this.friendRankNode.getChildByName('Nav')
  77. this.friendBtn = this.navNode.getChildByName('FriendBtn');
  78. this.worldBtn = this.navNode.getChildByName('WorldBtn');
  79. this.friendBtnBg = this.navNode.getChildByName('FriendHoverBg');
  80. this.worldBtnBg = this.navNode.getChildByName('WorldHoverBg');
  81. this.status = this.status || 1;
  82. this.loading = 0;
  83. this.updateTimer = 0;
  84. this.updateInterval = 0.2;
  85. this.lastContentPosY = 0; // use this variable to detect if we are scrolling up or down
  86. },
  87. start() {
  88. this.initScrolll();
  89. },
  90. onEnable() {
  91. console.log('rank enable')
  92. this.initialize();
  93. this.getWorldRankData(() => {
  94. this.renderWorldData()
  95. })
  96. },
  97. initScrolll() {
  98. this.worldScrollView.on('scrolling', function () {
  99. if (this.loading) return;
  100. var maxOffset = this.worldScrollViewComponent.getMaxScrollOffset().y,
  101. offset = this.worldScrollViewComponent.getScrollOffset().y
  102. if (maxOffset - offset <= 300) {
  103. this.getWorldRankData()
  104. }
  105. }, this)
  106. },
  107. initialize() {
  108. this.items = [];
  109. this.worldRankData = [];
  110. this.content.removeAllChildren();
  111. this.worldScrollViewComponent.scrollToTop();
  112. this.content.height = this.totalCount * this._viewitemTemp.height + this.spacing * 2; // get total content height
  113. },
  114. getWorldRankData(callback) {
  115. this.loading = true;
  116. let startIndex = this.worldRankData.length
  117. for (var i = 0; i < 50; i++) {
  118. this.worldRankData.push({
  119. index: i + startIndex
  120. })
  121. }
  122. this.content.height = this.worldRankData.length * this._viewitemTemp.height + this.spacing * 2; // get total content height
  123. callback && callback()
  124. this.scheduleOnce(() => {
  125. this.loading = false
  126. }, 1);
  127. },
  128. renderWorldData() {
  129. for (let i = 0; i < this.spawnCount; ++i) { // spawn items, we only need to do this once
  130. let item = cc.instantiate(this._viewitemTemp);
  131. item.setPosition(0, -item.height * (0.5 + i) - this.spacing);
  132. this.updateNode(item, i)
  133. this.content.addChild(item);
  134. this.items.push(item);
  135. item.itemID = i;
  136. }
  137. },
  138. updateNode(node, index) {
  139. if (index <= 2) {
  140. node.getChildByName('ranknunbg' + index).active = true
  141. node.getChildByName('ranknum').active = false
  142. } else {
  143. node.getChildByName('ranknunbg0').active = false
  144. node.getChildByName('ranknunbg1').active = false
  145. node.getChildByName('ranknunbg2').active = false
  146. node.getChildByName('ranknum').active = true
  147. }
  148. node.getChildByName('ranknum').getComponent(cc.Label).string = index + 1
  149. node.getChildByName('username').getComponent(cc.Label).string = 'user' + (index + 1)
  150. node.getChildByName('CheckMark').active = ((index % 2) != 0);
  151. node.itemID = index
  152. },
  153. onFriendBtn() {
  154. if (this.status == 1) return;
  155. this.status = 1;
  156. },
  157. onWorldBtn() {
  158. if (this.status == 2) return;
  159. this.status = 2;
  160. },
  161. // get item position in scrollview's node space
  162. getPositionInView: function (item) {
  163. let worldPos = item.parent.convertToWorldSpaceAR(item.position);
  164. let viewPos = this.worldScrollView.convertToNodeSpaceAR(worldPos);
  165. return viewPos;
  166. },
  167. update(dt) {
  168. this.updateTimer += dt;
  169. if (this.updateTimer < this.updateInterval) return;
  170. this.updateTimer = 0;
  171. let items = this.items;
  172. let buffer = this.bufferZone;
  173. let isDown = this.content.y < this.lastContentPosY; // scrolling direction
  174. let offset = this._viewitemTemp.height * items.length;
  175. for (let i = 0; i < items.length; ++i) {
  176. let viewPos = this.getPositionInView(items[i]);
  177. if (isDown) {
  178. // if away from buffer zone and not reaching top of content
  179. if (viewPos.y < -buffer && items[i].y + offset < 0) {
  180. items[i].setPositionY(items[i].y + offset);
  181. let itemId = items[i].itemID - items.length; // update item id
  182. this.updateNode(items[i], itemId)
  183. }
  184. } else {
  185. // if away from buffer zone and not reaching bottom of content
  186. if (viewPos.y > buffer && items[i].y - offset > -this.content.height) {
  187. items[i].setPositionY(items[i].y - offset)
  188. let itemId = items[i].itemID + items.length
  189. this.updateNode(items[i], itemId)
  190. }
  191. }
  192. }
  193. this.lastContentPosY = this.worldScrollViewComponent.content.y;
  194. }
  195. });