Shop.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  7. import ShopItem from "../prefabs/ShopItem";
  8. import GameM, { AUDIO_TYPE, VIDEO_TYPE } from "../manager/GameM";
  9. import AdM from "../manager/AdM";
  10. import UiM, { PANEL_NAME } from "../manager/UiM";
  11. import { thirdVideoTS } from "../manager/topon/AnyThinkAdsMgr/thirdVideoTS";
  12. import EffectNode from "./EffectNode";
  13. import { Utils } from "../utils/Utils";
  14. const { ccclass, property } = cc._decorator;
  15. @ccclass
  16. export default class Shop extends cc.Component {
  17. @property(cc.Node)
  18. content: cc.Node = null;
  19. @property(cc.Node)
  20. sview: cc.Node = null;
  21. @property(cc.Node)
  22. mask: cc.Node = null;
  23. @property(cc.Node)
  24. hand: cc.Node = null;
  25. itemP = null
  26. //分帧加载
  27. addNum = 0
  28. addMax = 64
  29. addTotal = 64
  30. _n = 0
  31. _dis = 3
  32. private disY = 0
  33. private lastY = 0
  34. private hasScroll = false
  35. onLoad() {
  36. this.addNum = 0
  37. this.itemP = cc.loader.getRes('prefabs/ShopItem')
  38. }
  39. async onEnable() {
  40. this.itemP = await Utils.loadResPromise('prefabs/ShopItem')
  41. this.addNum = 0
  42. this.addTotal = Math.max(GameM.commonData.maxCarLevel - 6, 4)
  43. if (GameM.commonData.roleData.lv >= 3 && GameM.commonData.roleData.shop == 0) {
  44. // this.mask.active = true
  45. // this.hand.active = true
  46. }
  47. else {
  48. this.hideGuide()
  49. }
  50. this.content.height = (this.addTotal + 1) * 163
  51. this.addNum = this.addTotal
  52. this.hasScroll = false
  53. this.sview.on('touch-up', this.stopAutoScroll, this)
  54. this.sview.on('scroll-began', this.stopAutoScroll, this)
  55. // this.scheduleOnce(this.onAutoScroll, 1)
  56. this.onAutoScroll()
  57. AdM.showBanner()
  58. }
  59. onAutoScroll() {
  60. if (!this.hasScroll) {
  61. this.sview.getComponent(cc.ScrollView).scrollTo(cc.v2(this.content.height, 0), 0.6)
  62. }
  63. }
  64. stopAutoScroll() {
  65. this.hasScroll = true
  66. }
  67. hideGuide() {
  68. this.mask.active = false
  69. this.hand.active = false
  70. }
  71. onDisable() {
  72. AdM.destroyNative()
  73. this.sview.off('touch-up', this.stopAutoScroll, this)
  74. this.sview.off('scroll-began', this.stopAutoScroll, this)
  75. AdM.destroyBanner()
  76. }
  77. onScroll() {
  78. if (Math.abs(this.content.y - this.lastY) >= this.disY) {
  79. this.lastY = this.content.y
  80. this.content.children.forEach(element => {
  81. this.checkVisible(element)
  82. });
  83. }
  84. }
  85. refreshPrice(type) {
  86. let item = this.content.getChildByName(type.toString())
  87. if (item) {
  88. let s = item.getComponent(ShopItem)
  89. s.refreshPrice()
  90. }
  91. }
  92. start() {
  93. }
  94. onClickClose() {
  95. GameM.audioM.playEffect(AUDIO_TYPE.button)
  96. // this.node.active = false
  97. UiM.Instance.offPanel(PANEL_NAME.ShopNode, false, () => {
  98. AdM.createInter(3)
  99. })
  100. }
  101. update(dt) {
  102. if (this.addNum >= 0) {
  103. if (this._n <= this._dis) {
  104. this._n++
  105. }
  106. else {
  107. this._n = 0
  108. let item: cc.Node = cc.instantiate(this.itemP)
  109. item.name = (this.addNum + 1).toString()
  110. let s = item.getComponent(ShopItem)
  111. s.init(this.addNum + 1)
  112. this.disY = item.height + 10
  113. item.y = - item.height * 0.5 - (item.height + 10) * this.addNum
  114. this.content.addChild(item)
  115. this.checkVisible(item)
  116. // this.content.height = this.addTotal * (item.height + 10)
  117. this.addNum--
  118. }
  119. }
  120. this.onScroll()
  121. }
  122. checkVisible(item: cc.Node) {
  123. let disy = item.y + this.content.y
  124. if (disy >= 90 + this.disY || disy <= -this.sview.height - 90 - this.disY) {
  125. item.active = false
  126. }
  127. else {
  128. item.active = true
  129. }
  130. }
  131. }