Commit d24cfced authored by Tt's avatar Tt

循环重构

parent d2b6c858
...@@ -6,6 +6,14 @@ const { ccclass, property } = cc._decorator; ...@@ -6,6 +6,14 @@ const { ccclass, property } = cc._decorator;
let win: any = window; let win: any = window;
let courseware = win.courseware; let courseware = win.courseware;
enum State {
Idle,
Recording,
RecordEnd,
RecordShow,
Over
}
@ccclass @ccclass
export default class SceneComponent extends MyCocosSceneComponent { export default class SceneComponent extends MyCocosSceneComponent {
...@@ -45,10 +53,6 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -45,10 +53,6 @@ export default class SceneComponent extends MyCocosSceneComponent {
private currentId: number private currentId: number
//总时间时间 //总时间时间
private time: number private time: number
//获取起始执行时间
private startTime: number
//倒计时时间
private countdownTime: string
//正确答题数 //正确答题数
private right: number private right: number
//进度条成都 //进度条成都
...@@ -69,52 +73,19 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -69,52 +73,19 @@ export default class SceneComponent extends MyCocosSceneComponent {
} }
async initGame() { async initGame() {
this.showCamera();
await this.showBegin() await this.showBegin()
this.hideBegin(); this.hideBegin();
this.nextQuestion(); this.startQuestion();
} }
showCamera() { showCamera() {
// cw.openRecognitionCamera({ courseware && courseware.openRecognitionCamera({
// x: 0, x: 0,
// y: 0, y: 0,
// width: 1, width: 1,
// height: 1, height: 1,
// }) })
}
startRecord() {
courseware && courseware.startTest(''); //开始录音
}
endRecord() {
let testData = {
"dtLastResponse": "2021-09-17 13:36:26:109",
"refText": "I haven't found anything out about him yet.",
"recordId": "6144295213f0a2200000ed93",
"eof": 1,
"result": {
"pronunciation": 25,
"resource_version": "3.1.0",
"fluency": 60,
"rhythm": 90,
"kernel_version": "5.2.4",
"overall": 25,//75
"integrity": 100,
"duration": "6.779",
"rear_tone": "fall",
"speed": 96,
},
"tokenId": "6144295408558b08dd000001",
"applicationId": "154838659000009e"
}
if (!courseware) return this.recrodEnd(testData)
courseware && courseware.stopTest((data) => {
data = JSON.parse(data);
this.recrodEnd(data);
}); //结束录音
}
recrodEnd(data) {
let score = data.result.overall;
} }
//----------------------Ready Go------------------------------------ //----------------------Ready Go------------------------------------
showBegin() { showBegin() {
...@@ -155,89 +126,108 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -155,89 +126,108 @@ export default class SceneComponent extends MyCocosSceneComponent {
}); });
} }
//----------------------Question------------------------------------ //----------------------Question------------------------------------
nextQuestion() { private currentTime: number;
startQuestion() {
// 游戏所需的初始化数据 // 游戏所需的初始化数据
this.currentId = 0 this.currentId = 0
this.time = this.data.options.length * 3 this.time = this.data.options.length * 3
this.startTime = this.time * 1000 + Date.now() this.currentTime = this.time * 1000;
this.countdownTime = "60.00"
this.lengthProgress = 0 this.lengthProgress = 0
this.right = 0 this.right = 0
//预加载第一题 //预加载第一题
this.firstOptions() this.firstOptions()
//题目数分配 //题目数分配
this.questionNuber() this.updateProgress()
} }
//预加载第一题 //预加载第一题
firstOptions() { firstOptions() {
var options = this.data.options
let topQuestion = pg.view.find(this, 'layer_game/topQuestion'); let topQuestion = pg.view.find(this, 'layer_game/topQuestion');
let countdown = pg.view.find(this, 'layer_game/countdown');
let countdown1 = pg.view.find(countdown, 'countdown1');
let countdown2 = pg.view.find(countdown, 'countdown2');
countdown1.active = true
countdown2.active = false
this.startCountDown = true;
this.state = State.Recording;
this.updateTitleOptions();
}
private startCountDown: boolean;
update(dt) {
if (this.startCountDown) {
var nowTime = Date.now();
if (!this.lastTime) this.lastTime = nowTime;
let subTime = nowTime - this.lastTime;
this.currentTime -= subTime;
this.lastTime = nowTime
if (this.currentTime < 0) {
this.currentTime = 0;
// 游戏结束
}
this.updateClock(subTime);
this.updateTime(subTime);
pg.view.setNetImg(topQuestion.children[1], options[this.currentId].image, { w: 700, h: 360 })
pg.view.setString(topQuestion.children[2], options[this.currentId].text)
//倒计时
this.countdownQuestion(topQuestion)
} }
//倒计时
countdownQuestion(topQuestion) {
let countdown = pg.view.find(this, 'layer_game/countdown');
var chil = countdown.children
var b = this.time - 3
chil[0].active = true
chil[1].active = false
var timer = setInterval(() => {
var currentTime = Date.now();
var timePassed = this.startTime - currentTime;
this.countdownTime = String(timePassed / 1000)
//更换时间及闹钟图片
if (Number(this.countdownTime) >= 10) {
chil[0].active = true
pg.view.setString(chil[0].children[0], this.countdownTime)
} else {
chil[0].active = false
chil[1].active = true
pg.view.setString(chil[1].children[0], this.countdownTime)
} }
//每隔五秒 private state: number;
if (Number(this.countdownTime) - b <= 0) { private waitTime: number;
/* console.log(Number(this.countdownTime) - b) */ updateTime(dt) {
this.waitTime -= dt;
//等待用户回答问题 if (this.waitTime > 0) return;
this.whetherQuestions(topQuestion) switch (this.state) {
b -= 3 case State.Recording:
//下一题 this.state = State.RecordEnd;
setTimeout(() => { this.waitTime = 3 * 1000;//3秒录音
this.titleOptions(topQuestion) this.startRecord(this.getCurrentOptions().text);
//动画时间 break;
this.startTime += 1000 case State.RecordEnd:
}, 700); this.state = State.RecordShow;
this.waitTime = 1 * 1000;//1秒展示
this.endRecord();
break;
case State.RecordShow:
this.state = State.Recording;
this.updateTitleOptions()
break;
} }
//倒计时结束
if (Number(this.countdownTime) <= 0) {
clearInterval(timer)
this.countdownTime = "00.000"
pg.view.setString(chil[1].children[0], this.countdownTime)
} }
}, 100);
getCurrentOptions() {
return this.data.options[this.currentId];
} }
//题目 //题目
titleOptions(topQuestion) { updateTitleOptions() {
var options = this.data.options let topQuestion = pg.view.find(this, 'layer_game/topQuestion');
pg.view.setNetImg(topQuestion.children[1], options[this.currentId].image, { w: 700, h: 360 }) pg.view.setNetImg(topQuestion.children[1], this.getCurrentOptions().image, { w: 700, h: 360 })
pg.view.setString(topQuestion.children[2], options[this.currentId].text) pg.view.setString(topQuestion.children[2], this.getCurrentOptions().text)
console.log('next') }
// 倒计时完成
private lastTime: number;
updateClock(dt) {
let time = Math.floor(this.currentTime / 100);
let n1 = Math.floor(time / 100) > 0 ? Math.floor(time / 100) : 0;
time -= (n1 * 100);
let n2 = Math.floor(time / 10) > 0 ? Math.floor(time / 10) : 0;
time -= (n2 * 10);
let n3 = Math.floor(time / 1) > 0 ? Math.floor(time / 1) : 0;
let countStr = `${n1}${n2}.${n3}`;
let countdown = pg.view.find(this, 'layer_game/countdown');
let countdown1 = pg.view.find(countdown, 'countdown1');
let countdown2 = pg.view.find(countdown, 'countdown2');
//更换时间及闹钟图片
countdown1.active = (Number(countStr) >= 10)
countdown2.active = !(Number(countStr) >= 10)
pg.view.setString(countdown1.children[0], countStr)
pg.view.setString(countdown2.children[0], countStr)
} }
//题目数分配 //题目数分配
questionNuber() { updateProgress() {
let ProgressBar = pg.view.find(this, 'layer_game/topProgressBar') let ProgressBar = pg.view.find(this, 'layer_game/topProgressBar')
var questionLenght = this.data.options.length var questionLenght = this.data.options.length
//星星数 //星星数
for (let i = 1; i <= 3; i++) { for (let i = 1; i <= 3; i++) {
ProgressBar.children[2].children[5 + i].active = true ProgressBar.children[2].children[5 + i].active = true
pg.view.setString(ProgressBar.children[2].children[5 + i], Math.round(questionLenght / 3 * i)) pg.view.setString(ProgressBar.children[2].children[5 + i], Math.round(questionLenght / 3 * i))
} }
//题目少于三题星星数 //题目少于三题星星数
/* if (questionLenght >= 3) { /* if (questionLenght >= 3) {
...@@ -257,13 +247,45 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -257,13 +247,45 @@ export default class SceneComponent extends MyCocosSceneComponent {
pg.view.setString(ProgressBar.children[2].children[8], Math.round(questionLenght)) pg.view.setString(ProgressBar.children[2].children[8], Math.round(questionLenght))
} */ } */
} }
//用户回答问题
whetherQuestions(topQuestion) {
var answer = this.currentId >= 0 startRecord(letter) {
this.whetherAnswer(topQuestion, answer) courseware && courseware.startTest(letter); //开始录音
}
endRecord() {
let testData = {
"dtLastResponse": "2021-09-17 13:36:26:109",
"refText": "I haven't found anything out about him yet.",
"recordId": "6144295213f0a2200000ed93",
"eof": 1,
"result": {
"pronunciation": 25,
"resource_version": "3.1.0",
"fluency": 60,
"rhythm": 90,
"kernel_version": "5.2.4",
"overall": 74,//75
"integrity": 100,
"duration": "6.779",
"rear_tone": "fall",
"speed": 96,
},
"tokenId": "6144295408558b08dd000001",
"applicationId": "154838659000009e"
}
if (!courseware) return this.recrodEnd(testData)
courseware && courseware.stopTest((data) => {
data = JSON.parse(data);
this.recrodEnd(data);
}); //结束录音
}
recrodEnd(data) {
let score = data.result.overall;
this.whetherAnswer(score > 40)
} }
//用户回答结果 //用户回答结果
whetherAnswer(topQuestion, answer) { whetherAnswer(answer) {
let topQuestion = pg.view.find(this, 'layer_game/topQuestion');
/* /*
* answer 获取的用户答案 * answer 获取的用户答案
*/ */
...@@ -276,7 +298,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -276,7 +298,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.rightQuestions(row) this.rightQuestions(row)
} else { } else {
//错误音频 //错误音频
pg.audio.playAudioByUrl("http://staging-teach.cdn.ireadabc.com/cb76dbc838567b58e2af36e2d2faa923_l.mp3") pg.audio.playAudioByUrl(this.getCurrentOptions().audio);
//错误 //错误
row.children[1].active = true row.children[1].active = true
} }
...@@ -312,7 +334,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -312,7 +334,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
row.children[0].active = true row.children[0].active = true
} }
addAnimation(row, topQuestion) { addAnimation(row, topQuestion) {
if (this.currentId < (this.time / 3) - 1) { if (this.currentId < (this.time / 5) - 1) {
//初始化 //初始化
setTimeout(() => { setTimeout(() => {
row.children[0].active = false row.children[0].active = false
......
export const defaultData = export const defaultData =
{ "onlineFlg": false, "title": "钓鱼大作战", "questionText": "海洋里有很多鱼,每条鱼身上都藏着魔法信息!亲爱的小玩家,请仔细听游戏指令,找到含有正确魔法信息的鱼,快速完成钓鱼。 在本次的游戏中,你将和其他玩家进行PK,游戏结束,钓鱼数量最多的玩家,将获得胜利。最后根据钓鱼的数量,你将获得相应的能量石奖励!开始挑战吧!", "questionTextAudio": "http://staging-teach.cdn.ireadabc.com/e422c682ff3304a532937db64573caa4.mp3", "questions": [{ "questionAudio": "http://staging-teach.cdn.ireadabc.com/cb76dbc838567b58e2af36e2d2faa923_l.mp3", "duration": 120, "options": [{ "type": "", "image": "http://staging-teach.cdn.ireadabc.com/3a7fdd71c900985e8f493d8e4035bf84.png", "audio": "http://staging-teach.cdn.ireadabc.com/c9db9c8f9c256b928a7f0e1498963404_l.mp3", "text": "111", "right": false, "audioName": "ready go.mp3" }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/017549d06d93f449af1ca4e1306cc970.png", "audio": "http://staging-teach.cdn.ireadabc.com/c9db9c8f9c256b928a7f0e1498963404_l.mp3", "text": "222", "right": false, "audioName": "ready go.mp3" }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/65732948abd635b143bcca6bf30e1ae3.png", "audio": "http://staging-teach.cdn.ireadabc.com/5eeac1448c75db6dda78519ae7986dd2_l.mp3", "text": "333", "right": false, "audioName": "星星变虎头.mp3" }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/c08d44585d0846bfe0fd8e923425d6a2.png", "audio": "http://staging-teach.cdn.ireadabc.com/5eeac1448c75db6dda78519ae7986dd2_l.mp3", "text": "444", "right": false, "audioName": "星星变虎头.mp3" }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/e6ea7e0efa41ddfbcbbd6e01581a1d2f.png", "audio": "http://staging-teach.cdn.ireadabc.com/cb76dbc838567b58e2af36e2d2faa923_l.mp3", "text": "555", "right": false, "audioName": "图片卡片出现.mp3" }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/3a7fdd71c900985e8f493d8e4035bf84.png", "audio": "http://staging-teach.cdn.ireadabc.com/c9db9c8f9c256b928a7f0e1498963404_l.mp3", "text": "111", "right": false, "audioName": "ready go.mp3" }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/017549d06d93f449af1ca4e1306cc970.png", "audio": "http://staging-teach.cdn.ireadabc.com/c9db9c8f9c256b928a7f0e1498963404_l.mp3", "text": "222", "right": false, "audioName": "ready go.mp3" }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/65732948abd635b143bcca6bf30e1ae3.png", "audio": "http://staging-teach.cdn.ireadabc.com/5eeac1448c75db6dda78519ae7986dd2_l.mp3", "text": "333", "right": false, "audioName": "星星变虎头.mp3" }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/c08d44585d0846bfe0fd8e923425d6a2.png", "audio": "http://staging-teach.cdn.ireadabc.com/5eeac1448c75db6dda78519ae7986dd2_l.mp3", "text": "444", "right": false, "audioName": "星星变虎头.mp3" }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/e6ea7e0efa41ddfbcbbd6e01581a1d2f.png", "audio": "http://staging-teach.cdn.ireadabc.com/cb76dbc838567b58e2af36e2d2faa923_l.mp3", "text": "555", "right": false, "audioName": "图片卡片出现.mp3" }], "audioName": "图片卡片出现.mp3" }], "bgAudio": "", "bgAudioName": "", "audioName": "" } {
"onlineFlg": false, "title": "钓鱼大作战",
"questionText": "",
"questionTextAudio": "http://staging-teach.cdn.ireadabc.com/e422c682ff3304a532937db64573caa4.mp3",
"questions": [
{
"questionAudio": "http://staging-teach.cdn.ireadabc.com/6d742645880f902efae878facc78da07_l.mp3", "duration": 120,
"options": [
{
"type": "img", "image": "http://staging-teach.cdn.ireadabc.com/5e0afcb2626cb97395fa80688d0da26e.png", "audio": "http://staging-teach.cdn.ireadabc.com/a5fb06cfafd5c74d143d00b8cafbed2e_l.mp3", "text": "push", "right": false, "audioName": "正确.mp3"
},
{ "type": "", "image": "http://staging-teach.cdn.ireadabc.com/d1905036a1cbe59e269641576bf025cb.png", "audio": "http://staging-teach.cdn.ireadabc.com/141bd6c7ce281deb3c2a6ceb66e2148e_l.mp3", "text": "yun", "right": false, "audioName": "错误.mp3" }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/65732948abd635b143bcca6bf30e1ae3.png", "audio": "http://staging-teach.cdn.ireadabc.com/5eeac1448c75db6dda78519ae7986dd2_l.mp3", "text": "right", "right": false, "audioName": "星星变虎头.mp3" }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/017549d06d93f449af1ca4e1306cc970.png", "audio": "http://staging-teach.cdn.ireadabc.com/6d742645880f902efae878facc78da07_l.mp3", "text": "star", "right": false, "audioName": "请在5秒内说出正确答案.mp3" }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/5e0afcb2626cb97395fa80688d0da26e.png", "audio": "", "text": "123", "right": false }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/e6ea7e0efa41ddfbcbbd6e01581a1d2f.png", "audio": "", "text": "123", "right": false }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/e6ea7e0efa41ddfbcbbd6e01581a1d2f.png", "audio": "", "text": "123", "right": false }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/5e0afcb2626cb97395fa80688d0da26e.png", "audio": "", "text": "123", "right": false }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/e6ea7e0efa41ddfbcbbd6e01581a1d2f.png", "audio": "", "text": "123", "right": false }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/5e0afcb2626cb97395fa80688d0da26e.png", "audio": "", "text": "123", "right": false }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/65732948abd635b143bcca6bf30e1ae3.png", "audio": "http://staging-teach.cdn.ireadabc.com/5eeac1448c75db6dda78519ae7986dd2_l.mp3", "text": "right", "right": false, "audioName": "星星变虎头.mp3" }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/d1905036a1cbe59e269641576bf025cb.png", "audio": "http://staging-teach.cdn.ireadabc.com/141bd6c7ce281deb3c2a6ceb66e2148e_l.mp3", "text": "yun", "right": false, "audioName": "错误.mp3" }, { "type": "", "image": "http://staging-teach.cdn.ireadabc.com/65732948abd635b143bcca6bf30e1ae3.png", "audio": "http://staging-teach.cdn.ireadabc.com/5eeac1448c75db6dda78519ae7986dd2_l.mp3", "text": "right", "right": false, "audioName": "星星变虎头.mp3" }], "audioName": "请在5秒内说出正确答案.mp3"
}], "bgAudio": "", "bgAudioName": "", "audioName": ""
}
// { // {
// "recordFlag": true, // "recordFlag": true,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment