| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614 |
- import {
- _decorator,
- assetManager,
- Button,
- Component,
- director,
- instantiate,
- Label,
- log,
- Node,
- Prefab,
- ScrollView,
- Size,
- size,
- Sprite,
- SpriteFrame,
- tween,
- UIOpacity,
- UITransform,
- v2,
- v3,
- Vec3
- } from 'cc';
- import {NodeUtils} from '../../script/utils/NodeUtils';
- import {LoadUtils} from '../../script/utils/LoadUtils';
- import {ArrUtils} from '../../script/utils/ArrUtils';
- import {UserInfo} from '../../script/UserInfo';
- import {BlindsTransitionNodes} from '../../script/utils/BlindsTransitionNodes';
- import {IntroMgr} from './IntroMgr';
- import {TimeProgress} from './TimeProgress';
- import {AudioManage} from './AudioManage';
- import {formatDate} from '../../script/utils/TimeUtils';
- const {ccclass, property} = _decorator;
- @ccclass('TestMgr')
- export class TestMgr extends Component {
- private stageLevel: number = 1
- private maxLevel: number = 5
- private turnLevel: number = 0
- private timeLimit: number = 5
- private timeLimitMax: number = 20
- private timeStep: number = 0
- private timeInterval: number = 1
- private timeStart: boolean = false
- private score: number = 0
- private scoreMax: number = 4
- private timeArr: number[] = new Array()//答题时间数组,用于计算平均答题时间
- private waitOpTime: number = 8//主画面出现后,等待选项出现的时间
- private totalRight: number = 0
- private totalWrong: number = 0
- private totalTimeout: number = 0
- private totalQustion: number = 0
- @property([SpriteFrame])
- private btnSprite: SpriteFrame[] = new Array()
- @property(Prefab)
- private btnPre: Prefab = null
- @property(Prefab)
- private btnPre2: Prefab = null
- @property(Prefab)
- private scrollTip: Prefab = null
- private isShowIntro: boolean = false
- private detail: any = {}
- private details: any[] = new Array()
- // private timuTotal:number = 0
- // private rightCnt:number = 0//z
- /**
- * 图片有错误的
- * 1-5
- * 2-0
- * 2-2
- * 3-5
- */
- private optionArr: any[][] = [
- [],
- [//5
- {//1
- txt: "哪张图片上的物体位置与刚才不同?",
- itmes: [{index: 4, isRight: false}, {index: 5, isRight: false}, {index: 6, isRight: true}, {
- index: 7,
- isRight: false
- }, {index: 7, isRight: false}],
- rightCnt: 1
- },
- {//2
- txt: "哪张图片上的物体位置与刚才不同?",
- itmes: [{index: 4, isRight: false}, {index: 5, isRight: false}, {index: 6, isRight: true}, {
- index: 7,
- isRight: false
- }, {index: 7, isRight: false}],
- rightCnt: 1
- },
- {//3
- txt: "哪张图片上的物体位置与刚才不同?",
- itmes: [{index: 4, isRight: false}, {index: 5, isRight: false}, {index: 6, isRight: true}, {
- index: 7,
- isRight: false
- }, {index: 7, isRight: false}],
- rightCnt: 1
- },
- {//4
- txt: "哪张图片上的物体位置与刚才不同?",
- itmes: [{index: 4, isRight: false}, {index: 5, isRight: false}, {index: 6, isRight: true}, {
- index: 7,
- isRight: false
- }, {index: 7, isRight: false}],
- rightCnt: 1
- },
- {//5
- txt: "哪张图片上的物体位置与刚才不同?",
- itmes: [{index: 4, isRight: false}, {index: 5, isRight: false}, {index: 6, isRight: true}, {
- index: 7,
- isRight: false
- }, {index: 7, isRight: false}],
- rightCnt: 1
- },
- {//6
- txt: "哪张图片上的物体位置与刚才不同?",
- itmes: [{index: 4, isRight: false}, {index: 5, isRight: false}, {index: 6, isRight: true}, {
- index: 7,
- isRight: false
- }, {index: 7, isRight: false}],
- rightCnt: 1
- },
- {//7
- txt: "哪张图片上的物体位置与刚才不同?",
- itmes: [{index: 4, isRight: false}, {index: 5, isRight: false}, {index: 6, isRight: true}, {
- index: 7,
- isRight: false
- }, {index: 7, isRight: false}],
- rightCnt: 1
- },
- {//8
- txt: "哪张图片上的物体位置与刚才不同?",
- itmes: [{index: 4, isRight: false}, {index: 5, isRight: false}, {index: 6, isRight: true}, {
- index: 7,
- isRight: false
- }, {index: 7, isRight: false}],
- rightCnt: 1
- },
- {//9
- txt: "哪张图片上的物体位置与刚才不同?",
- itmes: [{index: 4, isRight: false}, {index: 5, isRight: false}, {index: 6, isRight: true}, {
- index: 7,
- isRight: false
- }, {index: 7, isRight: false}],
- rightCnt: 1
- },
- {//10
- txt: "哪张图片上的物体位置与刚才不同?",
- itmes: [{index: 4, isRight: false}, {index: 5, isRight: false}, {index: 6, isRight: true}, {
- index: 7,
- isRight: false
- }, {index: 7, isRight: false}],
- rightCnt: 1
- },
- {//11
- txt: "哪张图片上的物体位置与刚才不同?",
- itmes: [{index: 4, isRight: false}, {index: 5, isRight: false}, {index: 6, isRight: true}, {
- index: 7,
- isRight: false
- }, {index: 7, isRight: false}],
- rightCnt: 1
- },
- {//12
- txt: "哪张图片上的物体位置与刚才不同?",
- itmes: [{index: 4, isRight: false}, {index: 5, isRight: false}, {index: 6, isRight: true}, {
- index: 7,
- isRight: false
- }, {index: 7, isRight: false}],
- rightCnt: 1
- },
- {//13
- txt: "哪张图片上的物体位置与刚才不同?",
- itmes: [{index: 4, isRight: false}, {index: 5, isRight: false}, {index: 6, isRight: true}, {
- index: 7,
- isRight: false
- }, {index: 7, isRight: false}],
- rightCnt: 1
- },
- {//14
- txt: "哪张图片上的物体位置与刚才不同?",
- itmes: [{index: 4, isRight: false}, {index: 5, isRight: false}, {index: 6, isRight: true}, {
- index: 7,
- isRight: false
- }, {index: 7, isRight: false}],
- rightCnt: 1
- },
- {//15
- txt: "哪张图片上的物体位置与刚才不同?",
- itmes: [{index: 4, isRight: false}, {index: 5, isRight: false}, {index: 6, isRight: true}, {
- index: 7,
- isRight: false
- }, {index: 7, isRight: false}],
- rightCnt: 1
- },
- ]
- ]
- protected onLoad(): void {
- this.timeStart = false
- this.timeStep = 0
- this.timeArr = []
- this.node.getChildByName("titleBg").getChildByName("startBtn").off("click")
- this.node.getChildByName("titleBg").getChildByName("startBtn").on("click", () => {
- this.begin()
- }, this)
- this.isShowIntro = true
- }
- start() {
- this.score = 0
- this.initGame()
- }
- update(deltaTime: number) {
- if (this.timeStart) {
- this.timeStep += deltaTime
- }
- }
- private cleanTime() {
- this.timeStart = false
- this.timeStep = 0
- }
- private canClick: boolean = true
- private isGameing: boolean = true
- private rightCnt: number = 0
- private rightNodeArr: Node[] = new Array()
- //初始化游戏
- private initGame() {
- this.cleanTime()
- // this.score = 0
- this.isGameing = true
- this.canClick = true
- this.rightCnt = 0
- this.rightNodeArr = []
- this.node.getChildByName("Label").active = false
- this.node.getChildByName("noclick").active = true
- this.node.getChildByName("noclick2").active = true
- this.node.getChildByName("loadSpine").active = true
- this.node.getChildByName("content").getChildByName("Layout").removeAllChildren()
- this.node.getChildByName("content").active = false
- this.node.getChildByName("icon").getComponent(Sprite).spriteFrame = null
- this.node.getChildByName("icon").active = false
- this.node.getChildByName("icon").getChildByName("Label").active = false
- this.node.getChildByName("progress").active = false
- this.node.getChildByName("progress").getComponent(TimeProgress).prepair(this.timeLimitMax)
- // this.closeTitle()
- this.node.getChildByName("Label2").getComponent(Label).string = this.stageLevel + "-" + this.turnLevel + "--" + this.score
- this.node.getChildByName("titleBg").active = false
- this.node.getChildByName("titleBg").getChildByName("Label2").getComponent(Label).string = ""
- this.node.getChildByName("titleBg").getChildByName("Label3").getComponent(Label).string = ""
- this.scheduleOnce(function () {
- if (this.isShowIntro) {
- this.showIntro()
- // this.showTitle()
- } else {
- this.node.getChildByName("intro").active = false
- this.showTitle()
- }
- }, 0.5);
- }
- private showIntro() {
- this.isShowIntro = false
- const content = this.node.getChildByName("intro").getChildByName("ScrollView").getChildByName("view").getChildByName("content")
- const len = content.children.length
- this.node.getChildByName("intro").getChildByName("Layout").removeAllChildren()
- for (let i = 0; i < len; i++) {
- const n_st = instantiate(this.scrollTip)
- if (i == 0) {
- n_st.getChildByName("icon").active = true
- }
- this.node.getChildByName("intro").getChildByName("Layout").addChild(n_st)
- }
- this.node.getChildByName("intro").active = true
- NodeUtils.maoPaoShow(this.node.getChildByName("intro"), () => {
- // this.goIntro(len)
- this.node.getChildByName("intro").getComponent(IntroMgr).startIntro()
- })
- }
- public showTitle() {
- AudioManage.instance.pauseMusic()
- this.scheduleOnce(function () {
- AudioManage.instance.playSoundCallback("sound/intro4")
- }, 0.5);
- // if (this.turnLevel == 0) {
- this.node.getChildByName("titleBg").getChildByName("Label2").getComponent(Label).string = "第" + (this.turnLevel + 1) + "题"
- this.node.getChildByName("titleBg").getChildByName("Label3").getComponent(Label).string = "您有" + this.waitOpTime + "秒时间观察图片,之后在" + this.timeLimitMax + "秒时间内从4个选项中选出正确答案。"
- // this.node.getChildByName("titleBg").getChildByName("startBtn").active = true
- NodeUtils.maoPaoShow(this.node.getChildByName("titleBg"), () => {
- })
- // } else {
- // this.begin()
- // }
- }
- private closeTitle() {
- AudioManage.instance.pauseMusic()
- this.node.getChildByName("titleBg").active = false
- // this.node.getChildByName("titleBg").getChildByName("bg").active = false
- // this.node.getChildByName("titleBg").getChildByName("startBtn").active = false
- }
- private begin() {
- this.detail.topicStartTime = new Date()//记录题目开始时间
- this.closeTitle()
- // this.node.getChildByName("content").getChildByName("Label").getComponent(Label).string = this.optionArr[this.stageLevel][this.turnLevel].txt
- this.node.getChildByName("content").getChildByName("Label").getComponent(Label).string = ""
- this.node.getChildByName("noclick2").active = true
- this.node.getChildByName("loadSpine").active = true
- const index = this.genCenterPic("图例", true)
- }
- /**
- * 生成题目的主图片
- * @param name 图片名字(URL路径名称)
- * @param isGen 默认false。如果设置成true则生成主图片后会继续生成按钮图片
- * @returns
- */
- private genCenterPic(name: string, isGen: boolean = false, callback: Function = null) {
- LoadUtils.loadBundleRes("distance", "resources/texture/question/a (" + (this.turnLevel + 1) + ")/" + name + "/spriteFrame", (asset) => {
- this.node.getChildByName("icon").getComponent(Sprite).spriteFrame = asset
- // this.node.getChildByName("noclick2").active = false
- // this.scheduleOnce(function () {
- // tween(this.node.getChildByName("icon")).to(0.5, { position: v3(0, 0) }).start()
- // }, 0.5);
- if (isGen) {
- // this.genBtn(index)
- let tarr: Node[] = new Array()
- this.genBtn2(0, tarr)
- }
- if (null != callback) {
- callback()
- }
- }, null, SpriteFrame)
- // return index
- }
- /**
- * 从第5关开始,生成侧面的按钮
- * @param index
- * @param tarr
- * @param folderIndex
- */
- private genBtn2(index: number = 0, tarr: Node[]) {
- index++
- if (index <= 4) {
- LoadUtils.loadBundleRes("distance", "resources/texture/question/a (" + (this.turnLevel + 1) + ")/" + index + "/spriteFrame", (asset) => {
- // this.node.getChildByName("icon").getComponent(Sprite).spriteFrame = asset
- const tnode: Node = instantiate(this.btnPre2)
- tnode.getChildByName("Mask").getChildByName("icon").getComponent(Sprite).spriteFrame = asset
- const ii = index
- tnode.off("click")
- tnode.on("click", (event) => {
- this.clicked(event, (index == 1), ii)
- }, this)
- tarr.push(tnode)
- if (index == 1) {
- this.rightNodeArr.push(tnode)
- }
- this.genBtn2(index, tarr)
- }, null, SpriteFrame)
- } else {
- //测试的时候暂时注释,让所有的第一个选项都是正确答案2025-10-09
- tarr = ArrUtils.shuffleArray(tarr)
- tarr.forEach((tnode) => {
- this.node.getChildByName("content").getChildByName("Layout").addChild(tnode)
- })
- this.waitOption()
- // this.beginTime()
- }
- }
- /**
- * 等待waitOptime秒,再开始出现答题选项
- */
- private waitOption() {
- this.node.getChildByName("noclick2").active = false
- this.node.getChildByName("loadSpine").active = false
- //主图片入场
- // this.node.getChildByName("icon").getComponent(BlindsTransitionNodes).setHide(false)
- this.node.getChildByName("icon").getComponent(BlindsTransitionNodes).setHide(true)
- this.node.getChildByName("icon").active = true
- // this.node.getChildByName("icon").getComponent(BlindsTransitionNodes).playReverse(() => {
- // // this.setCountdown(5)
- // })
- this.scheduleOnce(function () {//选项图入场
- // this.node.getChildByName("noclick2").active = true
- this.opShow()
- }, this.waitOpTime);
- }
- private opShow() {
- this.node.getChildByName("icon").getComponent(BlindsTransitionNodes).playByStep(() => {
- // console.log("cccccc");
- this.node.getChildByName("content").getComponent(BlindsTransitionNodes).setHide(false)
- this.node.getChildByName("content").active = true
- this.node.getChildByName("content").getComponent(BlindsTransitionNodes).playReverse(() => {
- this.beginTime()
- })
- })
- }
- /**
- * 开始答题
- */
- private beginTime() {
- this.cancelTime()
- this.timeStart = true
- this.node.getChildByName("noclick").active = false
- this.node.getChildByName("noclick2").active = false
- this.node.getChildByName("loadSpine").active = false
- // this.schedule(this.timeAlert, this.timeLimit, 0, this.timeLimit);
- this.detail.userTime1 = new Date().getTime()//记录用户答题开始时间
- this.schedule(this.timeOver, this.timeLimitMax, 0, this.timeLimitMax)
- this.node.getChildByName("progress").active = true
- this.node.getChildByName("progress").getComponent(TimeProgress).startProgress()
- }
- /**
- * 取消计时
- */
- private cancelTime() {
- // this.unschedule(this.timeAlert);
- this.unschedule(this.timeOver)
- }
- /**
- * 开始计时
- */
- private timeAlert() {
- this.node.getChildByName("Label").getComponent(Label).string = "请尽快作答"
- this.node.getChildByName("Label").active = true
- this.scheduleOnce(function () {
- this.node.getChildByName("Label").active = false
- }, 3);
- }
- /**
- * 超时,显示正确答案
- * 2025-09-29-不显示正确答案,直接进入下一题
- */
- private timeOver() {
- this.node.getChildByName("noclick").active = true
- this.detail.userTime2 = new Date().getTime()//记录用户答题结束时间
- this.detail.results = 2//记录用户答题是否正确
- this.detail.topicEndTime = new Date()//记录题目结束时间
- this.detail.userTime2 = new Date().getTime()//记录用户答题结束时间
- this.detail.topicMainPic = "图例"
- this.detail.topicRightPic = "1"
- this.detail.userChosePic = "-1"
- // this.rightNodeArr.forEach((tnode) => {
- // tnode.getChildByName("duigou").active = true
- // })
- // this.scheduleOnce(function () {
- this.node.getChildByName("progress").getComponent(TimeProgress).stopProgress()
- this.totalTimeout++
- this.totalQustion++
- this.next()
- // }, 0.5);
- }
- //点击答案
- private clicked(event, isRight: boolean, ii: number) {
- if (!this.isGameing) return
- if (!this.canClick) return
- this.canClick = false
- // this.detail.topicEndTime = formatDate(new Date(), "yyyy-MM-dd HH:mm:ss")//记录题目结束时间
- this.detail.topicEndTime = new Date()//记录题目结束时间
- this.detail.userTime2 = new Date().getTime()//记录用户答题结束时间
- this.detail.topicMainPic = "图例"
- this.detail.topicRightPic = "1"
- this.detail.userChosePic = ii + ""
- this.node.getChildByName("Label").active = false
- this.node.getChildByName("progress").getComponent(TimeProgress).stopProgress()
- this.totalQustion++
- if (isRight) {
- this.totalRight++
- this.rightCnt++
- this.detail.results = 0//记录用户答题是否正确
- event.target.getChildByName("duigou").active = true
- if (this.rightCnt >= this.optionArr[this.stageLevel][this.turnLevel].rightCnt) {
- this.cancelTime()
- this.node.getChildByName("noclick").active = true
- this.score += this.scoreMax
- this.next()
- } else {
- this.unschedule(this.timeAlert);
- }
- } else {
- this.totalWrong++
- this.detail.results = 1//记录用户答题是否正确
- this.cancelTime()
- // if (this.stageLevel < 5) {
- // this.genCenterPic("top")
- // } else {
- // this.genCenterPic("top")
- // }
- event.target.getChildByName("chacha").active = true
- this.node.getChildByName("noclick").active = true
- this.rightNodeArr.forEach((tnode) => {
- tnode.getChildByName("duigou").active = true
- })
- this.scheduleOnce(function () {
- this.next()
- }, 0.5);
- }
- this.scheduleOnce(function () {
- this.canClick = true
- // this.next()
- }, 0.5);
- }
- //回答正确后,进入下一轮
- private next() {
- const tdata: any = {
- "topicStartTime": this.detail.topicStartTime,
- "topicEndTime": this.detail.topicEndTime,
- "userTime": (this.detail.userTime2 - this.detail.userTime1) / 1000,
- "results": this.detail.results,
- "topicMainPic": this.detail.topicMainPic,
- "topicRightPic": this.detail.topicRightPic,
- "userChosePic": this.detail.userChosePic,
- "stageLevel": this.turnLevel + 1
- }
- // const aa =
- // console.log("00000000000= " + JSON.stringify(tdata));
- this.details.push(tdata)//记录答题数据
- // console.log(tdata);
- this.isGameing = false
- this.timeStart = false
- const t = this.timeStep > 15 ? 15 : this.timeStep
- this.timeArr.push(t)
- this.timeStep = 0
- this.node.getChildByName("noclick").active = true
- // this.setScore()
- this.turnLevel++
- if (this.turnLevel >= this.optionArr[this.stageLevel].length) {
- this.turnLevel = 0
- this.stageLevel++
- if ((this.stageLevel >= this.optionArr.length) || (this.stageLevel > this.maxLevel)) {//题目全部做完了
- console.log("题目全部做完了");
- this.setScore()
- } else {
- this.showSuccTip()
- }
- } else {
- this.showSuccTip()
- }
- }
- private showSuccTip() {
- this.scheduleOnce(function () {
- this.initGame()
- }, 1);
- }
- //设置分数显示
- private setScore() {
- let tTime: number = 0
- this.timeArr.forEach((val) => {
- tTime += val
- })
- const aveTime = tTime / this.timeArr.length
- const tScore = this.score
- UserInfo.score = tScore
- UserInfo.aveTime = aveTime
- UserInfo.totalRight = this.totalRight
- UserInfo.totalWrong = this.totalWrong
- UserInfo.totalTimeout = this.totalTimeout
- UserInfo.totalQuestion = this.totalQustion
- UserInfo.details = this.details
- // console.log("000000000= " + JSON.stringify(this.details));
- // console.log(JSON.stringify(this.timeArr));
- // console.log("00000000000= "+this.score+" "+aveTime);
- director.loadScene("sc_result")
- }
- public noclick() {
- }
- }
|