IntroMgr.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import {_decorator, Component, EventTouch, find, Node, ScrollView, v2, Vec2} from 'cc';
  2. import {TestMgr} from './TestMgr';
  3. import {AudioManage} from '../script/AudioManage';
  4. const {ccclass, property} = _decorator;
  5. @ccclass('IntroMgr')
  6. export class IntroMgr extends Component {
  7. private startPos: Vec2
  8. private step: number = 0
  9. private turnInterval: number = 10
  10. private readTime: number[] = [31, 21.5, 17.5]
  11. protected onLoad(): void {
  12. this.node.getChildByName("controller").on("click", () => {
  13. }, this)
  14. this.node.getChildByName("controller").on(Node.EventType.TOUCH_START, this.onTouchStart, this)
  15. this.node.getChildByName("controller").on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this)
  16. this.node.getChildByName("controller").on(Node.EventType.TOUCH_END, this.onTouchEnd, this)
  17. this.node.getChildByName("controller").on(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this)
  18. this.node.getChildByName("Button").off("click")
  19. this.node.getChildByName("Button").on("click", () => {
  20. this.unschedule(this.goIntro);
  21. this.node.active = false
  22. // this.showTitle()
  23. find("Canvas").getComponent(TestMgr).showTitle()
  24. // find("Canvas/noclick2").active = true
  25. }, this)
  26. this.node.getChildByName("soundBtn").off("click")
  27. this.node.getChildByName("soundBtn").on("click", () => {
  28. AudioManage.instance.pauseMusic()
  29. AudioManage.instance.playSoundCallback("sound/intro" + this.step)
  30. }, this)
  31. }
  32. start() {
  33. }
  34. update(deltaTime: number) {
  35. }
  36. public startIntro() {
  37. AudioManage.instance.pauseMusic()
  38. AudioManage.instance.playSoundCallback("sound/intro" + this.step)
  39. const len = this.node.getChildByName("Layout").children.length
  40. // this.goIntro(len)
  41. this.step = 0
  42. this.schedule(this.goIntro, this.readTime[this.step], Number.POSITIVE_INFINITY, 0);
  43. }
  44. private goIntro() {
  45. const len = this.node.getChildByName("Layout").children.length
  46. this.step++
  47. if (this.step >= len) {
  48. this.step = 0
  49. }
  50. this.turnCard()
  51. // console.log("000000000000= " + this.step);
  52. }
  53. private turnCard() {
  54. AudioManage.instance.pauseMusic()
  55. this.unschedule(this.goIntro);
  56. const len = this.node.getChildByName("Layout").children.length
  57. const offset = 1 / (len - 1)
  58. // console.log("000000000000= " + this.step);
  59. this.node.getChildByName("ScrollView").getComponent(ScrollView).scrollTo(v2(offset * this.step, 0), 0.5)
  60. this.node.getChildByName("Layout").children.forEach((tnode) => {
  61. tnode.getChildByName("icon").active = false
  62. })
  63. this.node.getChildByName("Layout").children[this.step].getChildByName("icon").active = true
  64. this.schedule(this.goIntro, this.readTime[this.step], Number.POSITIVE_INFINITY, 0);
  65. this.scheduleOnce(function () {
  66. AudioManage.instance.playSoundCallback("sound/intro" + this.step)
  67. }, 0.5);
  68. }
  69. protected onDisable(): void {
  70. this.disTouch()
  71. }
  72. public disTouch() {
  73. this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this)
  74. this.node.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this)
  75. this.node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this)
  76. this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this)
  77. }
  78. private onTouchStart(event: EventTouch) {
  79. this.startPos = event.getUILocation()
  80. }
  81. private onTouchMove(event: EventTouch) {
  82. }
  83. private onTouchEnd(event: EventTouch) {
  84. const endPos = event.getUILocation()
  85. if (this.startPos.x > endPos.x) {//向左移,下一张
  86. const len = this.node.getChildByName("Layout").children.length
  87. if ((this.step + 1) < len) {
  88. this.step++
  89. this.turnCard()
  90. }
  91. } else if (this.startPos.x < endPos.x) {//向右移,上一张
  92. const len = this.node.getChildByName("Layout").children.length
  93. if ((this.step - 1) >= 0) {
  94. this.step--
  95. this.turnCard()
  96. }
  97. }
  98. }
  99. private onTouchCancel(event: EventTouch) {
  100. const endPos = event.getUILocation()
  101. if (this.startPos.x > endPos.x) {//向左移,下一张
  102. const len = this.node.getChildByName("Layout").children.length
  103. if ((this.step + 1) < len) {
  104. this.step++
  105. this.turnCard()
  106. }
  107. } else if (this.startPos.x < endPos.x) {//向右移,上一张
  108. const len = this.node.getChildByName("Layout").children.length
  109. if ((this.step - 1) >= 0) {
  110. this.step--
  111. this.turnCard()
  112. }
  113. }
  114. }
  115. }