GameMain.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { _decorator, Component, instantiate, Label, Node, NodeEventType, Prefab, tween, v3 } from 'cc';
  2. import { Global } from '../Common/Global';
  3. import { GameManager } from '../Manager/GameManager';
  4. import { AudioManage } from '../Manager/AudioManage';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('GameMain')
  7. export class GameMain extends Component {
  8. @property({ type: [Node] })
  9. article: Node[] = [];
  10. @property({ type: [Node] })
  11. num: Node[] = [];
  12. @property({ type: [Prefab] })
  13. articleItems: Prefab[] = [];
  14. @property(Node)
  15. queding: Node = null;
  16. @property(Node)
  17. quxiao: Node = null;
  18. @property(Node)
  19. tijiao: Node = null;
  20. @property(Node)
  21. articles: Node = null;
  22. private xuanzhong = 0;
  23. private articleNum = [, [2, 1, 3, 1, 3], [3, 2, 2, 4, 1], [2, 3, 4, 3, 2], [3, 3, 4, 2, 4], [4, 3, 6, 5, 2]];
  24. private zhengqueArr = [];
  25. start() {
  26. for (let i = 0; i < 5; i++) {
  27. this.article[i].on(NodeEventType.TOUCH_END, (e) => {
  28. for (let j = 0; j < 5; j++) {
  29. this.article[j].getChildByName("xuanzhong").active = false;
  30. }
  31. e.target.getChildByName("xuanzhong").active = true;
  32. this.xuanzhong = e.target.name.split("_")[1] - 1;
  33. })
  34. }
  35. for (let i = 0; i < 10; i++) {
  36. this.num[i].on(NodeEventType.TOUCH_END, (e) => {
  37. let lab = this.article[Number(this.xuanzhong)].getChildByName("num");
  38. let num1 = Number(e.target.name.split("_")[1]);
  39. let num2 = lab.getComponent(Label).string == "?" ? "" : Number(lab.getComponent(Label).string);
  40. lab.getComponent(Label).string = num2 + "" + num1;
  41. });
  42. }
  43. this.queding.on(NodeEventType.TOUCH_END, (e) => {
  44. for (let j = 0; j < 5; j++) {
  45. this.article[j].getChildByName("xuanzhong").active = false;
  46. }
  47. this.xuanzhong++;
  48. if (this.xuanzhong > 4) {
  49. this.xuanzhong = 0;
  50. }
  51. this.article[this.xuanzhong].getChildByName("xuanzhong").active = true;
  52. })
  53. this.quxiao.on(NodeEventType.TOUCH_END, (e) => {
  54. let lab = this.article[Number(this.xuanzhong)].getChildByName("num");
  55. let str = lab.getComponent(Label).string.slice(0, -1);
  56. lab.getComponent(Label).string = str.length > 0 ? str : "?";
  57. })
  58. this.tijiao.on(NodeEventType.TOUCH_END, (e) => {
  59. let index = 0;
  60. for (let i = 0; i < 5; i++) {
  61. let lab = this.article[i].getChildByName("num");
  62. let str = lab.getComponent(Label).string;
  63. if (this.zhengqueArr[i] == Number(str)) {
  64. index++;
  65. } else {
  66. let node = this.article[i].getChildByName("xuanzhong");
  67. this.article[i].getChildByName("num").active = false;
  68. this.article[i].getChildByName("cha").active = true;
  69. node.active = true;
  70. setTimeout(() => {
  71. node.active = false;
  72. setTimeout(() => {
  73. node.active = true;
  74. setTimeout(() => {
  75. node.active = false;
  76. }, 100);
  77. }, 100);
  78. }, 100);
  79. }
  80. }
  81. if (index == 5) {
  82. GameManager.getInstance().addScore(100);
  83. AudioManage.instance.playSound(Global.Audio_chenggong);
  84. GameManager.getInstance().showTips('恭喜您全部答对了!');
  85. } else {
  86. GameManager.getInstance().gameFail();
  87. AudioManage.instance.playSound(Global.Audio_cuowu);
  88. GameManager.getInstance().showTips('仔细瞧瞧,您有选错的物品哦!');
  89. }
  90. })
  91. }
  92. /**
  93. * 初始化逻辑
  94. * 每次GameManager.StartGame()时都会调用
  95. */
  96. initUI() {
  97. this.xuanzhong = 0;
  98. this.zhengqueArr = [];
  99. this.articleNum = [, [2, 1, 3, 1, 3], [3, 2, 2, 4, 1], [2, 3, 4, 3, 2], [3, 3, 4, 2, 4], [4, 3, 6, 5, 2]];
  100. this.articles.removeAllChildren();
  101. for (let i = 0; i < 5; i++) {
  102. this.article[i].getChildByName("num").getComponent(Label).string = "?";
  103. this.article[i].getChildByName("num").active = true;
  104. this.article[i].getChildByName("xuanzhong").active = false;
  105. this.article[i].getChildByName("cha").active = false;
  106. }
  107. this.article[this.xuanzhong].getChildByName("xuanzhong").active = true;
  108. for (let i = 0; i < 5; i++) {
  109. let arr = this.articleNum[GameManager.getInstance().curLevel];
  110. let num = Global.getRandom(0, arr.length - 1);
  111. let len = arr[num];
  112. this.zhengqueArr.push(len);
  113. arr.splice(num, 1);
  114. for (let j = 0; j < len; j++) {
  115. let item = instantiate(this.articleItems[i]);
  116. this.articles.addChild(item);
  117. item.setPosition(v3(Global.getRandom(-600, 600), Global.getRandom(-350, 350)));
  118. }
  119. }
  120. }
  121. /**
  122. * 游戏结束后清理
  123. */
  124. gameOver() {
  125. }
  126. update(deltaTime: number) {
  127. }
  128. }