MoreGameBanner.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * @description 互推界面banner item类
  3. * @author kaka
  4. */
  5. import MoreGamePicItem from "./MoreGamePicItem";
  6. import PointItem from "./PointItem";
  7. const { ccclass, property } = cc._decorator;
  8. @ccclass
  9. export default class MoreGameBanner extends cc.Component {
  10. @property(cc.Node)
  11. dragArea: cc.Node = null
  12. @property(cc.Node)
  13. item1: cc.Node = null
  14. @property(cc.Node)
  15. item2: cc.Node = null
  16. @property(cc.Prefab)
  17. pointPrefab: cc.Prefab = null
  18. @property(cc.Node)
  19. pointLy: cc.Node = null
  20. @property(cc.Node)
  21. btnLeft: cc.Node = null
  22. @property(cc.Node)
  23. btnRight: cc.Node = null
  24. private startX = 0
  25. private coolTime = false
  26. private curSelDownItem: cc.Node = null
  27. private preDownItem = null
  28. private curIndex = 0
  29. private bannerDatas = null
  30. private curSelPoint: cc.Node = null
  31. private hasClick = false;
  32. start() {
  33. this.dragArea.on(cc.Node.EventType.TOUCH_START, this.onDragStart, this)
  34. this.dragArea.on(cc.Node.EventType.TOUCH_END, this.onDragEnd, this)
  35. }
  36. onDisable() {
  37. this.dragArea.off(cc.Node.EventType.TOUCH_START, this.onDragStart, this)
  38. this.dragArea.off(cc.Node.EventType.TOUCH_END, this.onDragEnd, this)
  39. }
  40. public async setItemData(bannerDatas) {
  41. this.bannerDatas = bannerDatas.item_data;
  42. if (this.bannerDatas && this.bannerDatas.length > 0) {
  43. for (var i = 0; i < this.bannerDatas.length; i++) {
  44. let point = cc.instantiate(this.pointPrefab)
  45. point.name = i.toString()
  46. this.pointLy.addChild(point)
  47. }
  48. }
  49. this.onOpenBigPicNode(0);
  50. }
  51. onOpenBigPicNode(index) {
  52. this.curIndex = index
  53. let data = this.bannerDatas[this.curIndex]
  54. this.curSelDownItem = this.item1
  55. this.curSelDownItem.getComponent(MoreGamePicItem).init(data)
  56. this.curSelDownItem.x = 0
  57. this.curSelDownItem.active = true
  58. this.item2.x = 750
  59. this.curSelPoint = this.pointLy.getChildByName(this.curIndex.toString())
  60. this.curSelPoint.getComponent(PointItem).setSel(true)
  61. this.hasClick = false
  62. if (this.bannerDatas.length > 1) {
  63. this.schedule(() => {
  64. if (!this.hasClick) {
  65. this.onSelDown(1)
  66. }
  67. }, 3, cc.macro.REPEAT_FOREVER, 7)
  68. }
  69. else {
  70. this.btnLeft.active = false
  71. this.btnRight.active = false
  72. }
  73. }
  74. onLeft() {
  75. mk.audio.playEffect('button');
  76. this.hasClick = true
  77. this.onSelDown(2)
  78. }
  79. onRight() {
  80. mk.audio.playEffect('button');
  81. this.hasClick = true
  82. this.onSelDown(1)
  83. }
  84. /**
  85. * 滑动选择
  86. * @param state 1 向左 2 向右
  87. */
  88. onSelDown(state) {
  89. if (this.coolTime) {
  90. return
  91. }
  92. let preTo = -750
  93. let data
  94. if (state == 1) {
  95. if (this.curIndex + 1 > this.bannerDatas.length - 1) {
  96. this.curIndex = 0
  97. }
  98. else {
  99. this.curIndex++
  100. }
  101. data = this.bannerDatas[this.curIndex]
  102. if (this.curSelDownItem == this.item1) {
  103. this.curSelDownItem = this.item2
  104. this.preDownItem = this.item1
  105. }
  106. else {
  107. this.curSelDownItem = this.item1
  108. this.preDownItem = this.item2
  109. }
  110. this.curSelDownItem.getComponent(MoreGamePicItem).init(data)
  111. this.curSelDownItem.x = 750
  112. preTo = -750
  113. }
  114. else if (state == 2) {
  115. if (this.curIndex - 1 < 0) {
  116. this.curIndex = this.bannerDatas.length - 1
  117. }
  118. else {
  119. this.curIndex--
  120. }
  121. data = this.bannerDatas[this.curIndex]
  122. if (this.curSelDownItem == this.item1) {
  123. this.curSelDownItem = this.item2
  124. this.preDownItem = this.item1
  125. }
  126. else {
  127. this.curSelDownItem = this.item1
  128. this.preDownItem = this.item2
  129. }
  130. this.curSelDownItem.getComponent(MoreGamePicItem).init(data)
  131. this.curSelDownItem.x = -750
  132. preTo = 750
  133. }
  134. this.coolTime = true
  135. cc.tween(this.preDownItem).to(0.4, { x: preTo }, cc.easeInOut(1)).start()
  136. cc.tween(this.curSelDownItem).to(0.4, { x: 0 }, cc.easeInOut(1)).call(() => {
  137. this.onSelPoint()
  138. this.coolTime = false
  139. }).start()
  140. }
  141. onDragStart(evt) {
  142. this.hasClick = true
  143. this.startX = evt.touch._point.x
  144. }
  145. onDragEnd(evt) {
  146. let end = evt.touch._point.x - this.startX
  147. if (Math.abs(end) > 20) {
  148. if (end > 0) {
  149. this.onSelDown(2)
  150. }
  151. else {
  152. this.onSelDown(1)
  153. }
  154. }
  155. }
  156. onSelPoint() {
  157. if (this.curSelPoint) {
  158. this.curSelPoint.getComponent(PointItem).setSel(false)
  159. }
  160. this.curSelPoint = this.pointLy.getChildByName(this.curIndex.toString())
  161. this.curSelPoint.getComponent(PointItem).setSel(true)
  162. }
  163. }