| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- import { _decorator, Component, instantiate, Label, Node, NodeEventType, Prefab, tween, v3 } from 'cc';
- import { Global } from '../Common/Global';
- import { GameManager } from '../Manager/GameManager';
- import { AudioManage } from '../Manager/AudioManage';
- const { ccclass, property } = _decorator;
- @ccclass('GameMain')
- export class GameMain extends Component {
- @property({ type: [Node] })
- article: Node[] = [];
- @property({ type: [Node] })
- num: Node[] = [];
- @property({ type: [Prefab] })
- articleItems: Prefab[] = [];
- @property(Node)
- queding: Node = null;
- @property(Node)
- quxiao: Node = null;
- @property(Node)
- tijiao: Node = null;
- @property(Node)
- articles: Node = null;
- private xuanzhong = 0;
- 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]];
- private zhengqueArr = [];
- start() {
- for (let i = 0; i < 5; i++) {
- this.article[i].on(NodeEventType.TOUCH_END, (e) => {
- for (let j = 0; j < 5; j++) {
- this.article[j].getChildByName("xuanzhong").active = false;
- }
- e.target.getChildByName("xuanzhong").active = true;
- this.xuanzhong = e.target.name.split("_")[1] - 1;
- })
- }
- for (let i = 0; i < 10; i++) {
- this.num[i].on(NodeEventType.TOUCH_END, (e) => {
- let lab = this.article[Number(this.xuanzhong)].getChildByName("num");
- let num1 = Number(e.target.name.split("_")[1]);
- let num2 = lab.getComponent(Label).string == "?" ? "" : Number(lab.getComponent(Label).string);
- lab.getComponent(Label).string = num2 + "" + num1;
- });
- }
- this.queding.on(NodeEventType.TOUCH_END, (e) => {
- for (let j = 0; j < 5; j++) {
- this.article[j].getChildByName("xuanzhong").active = false;
- }
- this.xuanzhong++;
- if (this.xuanzhong > 4) {
- this.xuanzhong = 0;
- }
- this.article[this.xuanzhong].getChildByName("xuanzhong").active = true;
- })
- this.quxiao.on(NodeEventType.TOUCH_END, (e) => {
- let lab = this.article[Number(this.xuanzhong)].getChildByName("num");
- let str = lab.getComponent(Label).string.slice(0, -1);
- lab.getComponent(Label).string = str.length > 0 ? str : "?";
- })
- this.tijiao.on(NodeEventType.TOUCH_END, (e) => {
- let index = 0;
- for (let i = 0; i < 5; i++) {
- let lab = this.article[i].getChildByName("num");
- let str = lab.getComponent(Label).string;
- if (this.zhengqueArr[i] == Number(str)) {
- index++;
- } else {
- let node = this.article[i].getChildByName("xuanzhong");
- this.article[i].getChildByName("num").active = false;
- this.article[i].getChildByName("cha").active = true;
- node.active = true;
- setTimeout(() => {
- node.active = false;
- setTimeout(() => {
- node.active = true;
- setTimeout(() => {
- node.active = false;
- }, 100);
- }, 100);
- }, 100);
- }
- }
- if (index == 5) {
- GameManager.getInstance().addScore(100);
- AudioManage.instance.playSound(Global.Audio_chenggong);
- GameManager.getInstance().showTips('恭喜您全部答对了!');
- } else {
- GameManager.getInstance().gameFail();
- AudioManage.instance.playSound(Global.Audio_cuowu);
- GameManager.getInstance().showTips('仔细瞧瞧,您有选错的物品哦!');
- }
- })
- }
- /**
- * 初始化逻辑
- * 每次GameManager.StartGame()时都会调用
- */
- initUI() {
- this.xuanzhong = 0;
- this.zhengqueArr = [];
- 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]];
- this.articles.removeAllChildren();
- for (let i = 0; i < 5; i++) {
- this.article[i].getChildByName("num").getComponent(Label).string = "?";
- this.article[i].getChildByName("num").active = true;
- this.article[i].getChildByName("xuanzhong").active = false;
- this.article[i].getChildByName("cha").active = false;
- }
- this.article[this.xuanzhong].getChildByName("xuanzhong").active = true;
- for (let i = 0; i < 5; i++) {
- let arr = this.articleNum[GameManager.getInstance().curLevel];
- let num = Global.getRandom(0, arr.length - 1);
- let len = arr[num];
- this.zhengqueArr.push(len);
- arr.splice(num, 1);
- for (let j = 0; j < len; j++) {
- let item = instantiate(this.articleItems[i]);
- this.articles.addChild(item);
- item.setPosition(v3(Global.getRandom(-600, 600), Global.getRandom(-350, 350)));
- }
- }
- }
- /**
- * 游戏结束后清理
- */
- gameOver() {
- }
- update(deltaTime: number) {
- }
- }
|