Commit ee34caf6 authored by 范雪寒's avatar 范雪寒

feat: setTimeout

parent c3ba2f7c
...@@ -20,15 +20,16 @@ cc.Class({ ...@@ -20,15 +20,16 @@ cc.Class({
this.initSize(); this.initSize();
}, },
showLog (str) { showLog(str) {
const node = cc.find('middleLayer'); const node = cc.find('middleLayer');
if(node){ if (node) {
node.getComponent('middleLayer').showLog(str); node.getComponent('middleLayer').showLog(str);
}else{ } else {
cc.log(str); cc.log(str);
} }
}, },
_timeoutIds: null,
_imageResList: null, _imageResList: null,
_audioResList: null, _audioResList: null,
_animaResList: null, _animaResList: null,
...@@ -36,6 +37,7 @@ cc.Class({ ...@@ -36,6 +37,7 @@ cc.Class({
this._imageResList = []; this._imageResList = [];
this._audioResList = []; this._audioResList = [];
this._animaResList = []; this._animaResList = [];
this._timeoutIds = [];
}, },
_designSize: null, // 设计分辨率 _designSize: null, // 设计分辨率
...@@ -257,18 +259,18 @@ cc.Class({ ...@@ -257,18 +259,18 @@ cc.Class({
dragonDisplay.playAnimation(this.animationName, 1);//animation所有的动画名称 dragonDisplay.playAnimation(this.animationName, 1);//animation所有的动画名称
icon_ex_txt.active = false; icon_ex_txt.active = false;
icon_ex.active = false; icon_ex.active = false;
setTimeout(() => { this._timeoutIds.push(setTimeout(() => {
pg.view.visible(icon_ex, true); pg.view.visible(icon_ex, true);
let icon_ex_word = this._stageData.word; let icon_ex_word = this._stageData.word;
this.wordAni(icon_ex_txt, icon_ex_word) this.wordAni(icon_ex_txt, icon_ex_word)
pg.audio.playAudioByUrl(this._stageData.sound); pg.audio.playAudioByUrl(this._stageData.sound);
}, 1000 * 2); }, 1000 * 2));
setTimeout(() => { this._timeoutIds.push(setTimeout(() => {
pg.view.visible(btn_record, true); pg.view.visible(btn_record, true);
pg.view.visible(btn_replay, true); pg.view.visible(btn_replay, true);
this.playSFX("audio_record"); this.playSFX("audio_record");
}, 1000 * 6.3); }, 1000 * 6.3));
setTimeout(() => { this._timeoutIds.push(setTimeout(() => {
pg.view.visible(icon_hand, true); pg.view.visible(icon_hand, true);
let action = cc.tween(icon_hand); let action = cc.tween(icon_hand);
action.sequence( action.sequence(
...@@ -276,7 +278,7 @@ cc.Class({ ...@@ -276,7 +278,7 @@ cc.Class({
cc.tween().by(0.5, { y: -50 }), cc.tween().by(0.5, { y: -50 }),
).repeatForever(); ).repeatForever();
action.start(); action.start();
}, 1000 * 6.3); }, 1000 * 6.3));
} else if (GameManager.getIns().getState() == 2) { } else if (GameManager.getIns().getState() == 2) {
pg.view.visible(icon_hand, false); pg.view.visible(icon_hand, false);
pg.view.visible(btn_record, false); pg.view.visible(btn_record, false);
...@@ -287,14 +289,14 @@ cc.Class({ ...@@ -287,14 +289,14 @@ cc.Class({
pg.view.visible(btn_record, false); pg.view.visible(btn_record, false);
pg.view.visible(btn_recording, false); pg.view.visible(btn_recording, false);
pg.view.visible(progress, false); pg.view.visible(progress, false);
setTimeout(() => { this._timeoutIds.push(setTimeout(() => {//4秒钟之后出现goodjob提示
pg.view.visible(end, true); pg.view.visible(end, true);
pg.view.visible(end_success, true); pg.view.visible(end_success, true);
this.playSFX("audio_goodjob"); this.playSFX("audio_goodjob");
}, 1000 * 0.5);//4秒钟之后出现goodjob提示 }, 1000 * 0.5));
setTimeout(() => { this._timeoutIds.push(setTimeout(() => {
this.nextStage(); this.nextStage();
}, 1000 * 4);//6秒钟之后进入下一关卡 }, 1000 * 4));//6秒钟之后进入下一关卡
} else if (GameManager.getIns().getState() == 4) { } else if (GameManager.getIns().getState() == 4) {
// 一局失败弹出tryagain,同时下方录音按钮明亮并出现提示 // 一局失败弹出tryagain,同时下方录音按钮明亮并出现提示
pg.view.visible(end, true); pg.view.visible(end, true);
...@@ -303,7 +305,7 @@ cc.Class({ ...@@ -303,7 +305,7 @@ cc.Class({
pg.view.visible(btn_record, true); pg.view.visible(btn_record, true);
pg.view.visible(progress, false); pg.view.visible(progress, false);
setTimeout(() => { this._timeoutIds.push(setTimeout(() => {
this.playSFX("audio_record"); this.playSFX("audio_record");
pg.view.visible(icon_hand, true); pg.view.visible(icon_hand, true);
let action = cc.tween(icon_hand); let action = cc.tween(icon_hand);
...@@ -312,7 +314,7 @@ cc.Class({ ...@@ -312,7 +314,7 @@ cc.Class({
cc.tween().by(0.5, { y: -50 }), cc.tween().by(0.5, { y: -50 }),
).repeatForever(); ).repeatForever();
action.start(); action.start();
}, 1000 * 1); }, 1000 * 1));
} else if (GameManager.getIns().getState() == 5) { } else if (GameManager.getIns().getState() == 5) {
...@@ -347,7 +349,7 @@ cc.Class({ ...@@ -347,7 +349,7 @@ cc.Class({
itemfirst.y = 17; itemfirst.y = 17;
itemfirst.getComponent(cc.RichText).string = strfirst1; itemfirst.getComponent(cc.RichText).string = strfirst1;
setTimeout(() => { this._timeoutIds.push(setTimeout(() => {
let times = 20; let times = 20;
let time = 0; let time = 0;
let inter = setInterval(() => { let inter = setInterval(() => {
...@@ -361,7 +363,7 @@ cc.Class({ ...@@ -361,7 +363,7 @@ cc.Class({
itemfirst.y = 0; itemfirst.y = 0;
item.getComponent(cc.RichText).string = str2; item.getComponent(cc.RichText).string = str2;
}, 30); }, 30);
}, 4100); }, 4100));
// setTimeout(() => { // setTimeout(() => {
// //全部变大 // //全部变大
// let str2 = `<size=150><color=#ffffff>${s0}</color><color=#0000>${s1}</color></size>` // let str2 = `<size=150><color=#ffffff>${s0}</color><color=#0000>${s1}</color></size>`
...@@ -381,7 +383,7 @@ cc.Class({ ...@@ -381,7 +383,7 @@ cc.Class({
this.initLayout() this.initLayout()
window.courseware && window.courseware.startTest(this._stageData.word); //开始录音 window.courseware && window.courseware.startTest(this._stageData.word); //开始录音
setTimeout(() => { this._timeoutIds.push(setTimeout(() => {
if (!window.courseware) return this.recrodEnd({ if (!window.courseware) return this.recrodEnd({
"dtLastResponse": "2021-09-17 13:36:26:109", "dtLastResponse": "2021-09-17 13:36:26:109",
"refText": "I haven't found anything out about him yet.", "refText": "I haven't found anything out about him yet.",
...@@ -406,7 +408,7 @@ cc.Class({ ...@@ -406,7 +408,7 @@ cc.Class({
data = JSON.parse(data); data = JSON.parse(data);
this.recrodEnd(data); this.recrodEnd(data);
}); //结束录音 }); //结束录音
}, 4000); }, 4000));
}, },
recrodEnd(data) { recrodEnd(data) {
let score = data.result.overall; let score = data.result.overall;
...@@ -486,9 +488,9 @@ cc.Class({ ...@@ -486,9 +488,9 @@ cc.Class({
pg.view.visible(end, false); pg.view.visible(end, false);
pg.view.visible(ant_ske, false); pg.view.visible(ant_ske, false);
pg.view.visible(ax_ske, false); pg.view.visible(ax_ske, false);
setTimeout(() => { this._timeoutIds.push(setTimeout(() => {
this.initStep2(); this.initStep2();
}, 2000); }, 2000));
}, },
initStep2() { initStep2() {
let btn_replay = pg.view.find(this, "btn_replay") let btn_replay = pg.view.find(this, "btn_replay")
...@@ -654,9 +656,9 @@ cc.Class({ ...@@ -654,9 +656,9 @@ cc.Class({
createStarAni() { createStarAni() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.showStar(); this.showStar();
setTimeout(() => { this._timeoutIds.push(setTimeout(() => {
resolve(''); resolve('');
}, 500); }, 500));
}); });
}, },
createFireworkAni() { createFireworkAni() {
...@@ -668,21 +670,30 @@ cc.Class({ ...@@ -668,21 +670,30 @@ cc.Class({
showFireworks( showFireworks(
cc.find('Canvas/firework_ani'), cc.find('Canvas/firework_ani'),
cc.find('Canvas/RibbonNodeBase').children, cc.find('Canvas/RibbonNodeBase').children,
cc.v2(0, -400), cc.v2(0, 1000), 200, 200 cc.v2(0, -400), cc.v2(0, 1000), 200, 200,
(id) => {
this._timeoutIds.push(id);
}
); );
showFireworks( showFireworks(
cc.find('Canvas/firework_ani'), cc.find('Canvas/firework_ani'),
cc.find('Canvas/RibbonNodeBase').children, cc.find('Canvas/RibbonNodeBase').children,
cc.v2(-600, -400), cc.v2(200, 1000), 200, 200 cc.v2(-600, -400), cc.v2(200, 1000), 200, 200,
(id) => {
this._timeoutIds.push(id);
}
); );
showFireworks( showFireworks(
cc.find('Canvas/firework_ani'), cc.find('Canvas/firework_ani'),
cc.find('Canvas/RibbonNodeBase').children, cc.find('Canvas/RibbonNodeBase').children,
cc.v2(600, -400), cc.v2(-200, 1000), 200, 200 cc.v2(600, -400), cc.v2(-200, 1000), 200, 200,
(id) => {
this._timeoutIds.push(id);
}
); );
setTimeout(() => { this._timeoutIds.push(setTimeout(() => {
resolve(''); resolve('');
}, 1000); }, 1000));
}); });
}, },
len: null, len: null,
...@@ -771,7 +782,9 @@ cc.Class({ ...@@ -771,7 +782,9 @@ cc.Class({
async showAllFirework(parentNode, nodeList) { async showAllFirework(parentNode, nodeList) {
for (let i = 0; i < 6; i++) { for (let i = 0; i < 6; i++) {
this.showFirework(cc.v2(0, -parentNode.height / 2), parentNode, nodeList, parentNode.width * 2 / 3, parentNode.height * 1.3); this.showFirework(cc.v2(0, -parentNode.height / 2), parentNode, nodeList, parentNode.width * 2 / 3, parentNode.height * 1.3);
await asyncDelay(0.1); await asyncDelay(0.1, (id) => {
this._timeoutIds.push(id);
});
} }
}, },
...@@ -834,4 +847,10 @@ cc.Class({ ...@@ -834,4 +847,10 @@ cc.Class({
return quadBase; return quadBase;
}, },
// 生命周期
onDestroy() {
this._timeoutIds.forEach(id => {
clearTimeout(id);
});
},
}); });
...@@ -94,13 +94,13 @@ export function getScaleRateBy2Node(baseNode, targetNode, maxFlag = true) { ...@@ -94,13 +94,13 @@ export function getScaleRateBy2Node(baseNode, targetNode, maxFlag = true) {
} }
} }
export function getDistance (start, end){ export function getDistance(start, end) {
var pos = cc.v2(start.x - end.x, start.y - end.y); var pos = cc.v2(start.x - end.x, start.y - end.y);
var dis = Math.sqrt(pos.x*pos.x + pos.y*pos.y); var dis = Math.sqrt(pos.x * pos.x + pos.y * pos.y);
return dis; return dis;
} }
export function playAudioByUrl(audio_url, cb=null) { export function playAudioByUrl(audio_url, cb = null) {
if (audio_url) { if (audio_url) {
cc.assetManager.loadRemote(audio_url, (err, audioClip) => { cc.assetManager.loadRemote(audio_url, (err, audioClip) => {
const audioId = cc.audioEngine.play(audioClip, false, 0.8); const audioId = cc.audioEngine.play(audioClip, false, 0.8);
...@@ -114,21 +114,21 @@ export function playAudioByUrl(audio_url, cb=null) { ...@@ -114,21 +114,21 @@ export function playAudioByUrl(audio_url, cb=null) {
} }
export function btnClickAnima(btn, time=0.15, rate=1.05) { export function btnClickAnima(btn, time = 0.15, rate = 1.05) {
btn.tmpScale = btn.scale; btn.tmpScale = btn.scale;
btn.on(cc.Node.EventType.TOUCH_START, () => { btn.on(cc.Node.EventType.TOUCH_START, () => {
cc.tween(btn) cc.tween(btn)
.to(time / 2, {scale: btn.scale * rate}) .to(time / 2, { scale: btn.scale * rate })
.start() .start()
}) })
btn.on(cc.Node.EventType.TOUCH_CANCEL, () => { btn.on(cc.Node.EventType.TOUCH_CANCEL, () => {
cc.tween(btn) cc.tween(btn)
.to(time / 2, {scale: btn.tmpScale}) .to(time / 2, { scale: btn.tmpScale })
.start() .start()
}) })
btn.on(cc.Node.EventType.TOUCH_END, () => { btn.on(cc.Node.EventType.TOUCH_END, () => {
cc.tween(btn) cc.tween(btn)
.to(time / 2, {scale: btn.tmpScale}) .to(time / 2, { scale: btn.tmpScale })
.start() .start()
}) })
} }
...@@ -174,15 +174,12 @@ export function playAudio(audioClip, cb = null) { ...@@ -174,15 +174,12 @@ export function playAudio(audioClip, cb = null) {
} }
} }
export async function asyncDelay(time) { export async function asyncDelay(time, func) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { const id = setTimeout(() => {
setTimeout(() => {
resolve(); resolve();
}, time * 1000); }, time * 1000);
} catch (e) { func && func(id);
reject(e);
}
}) })
} }
......
...@@ -125,15 +125,16 @@ export async function jelly(node) { ...@@ -125,15 +125,16 @@ export async function jelly(node) {
}); });
} }
export async function asyncDelay(time) { export async function asyncDelay(time, func) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
setTimeout(() => { const id = setTimeout(() => {
resolve(); resolve();
}, time * 1000); }, time * 1000);
func && func(id);
}) })
} }
export async function showFireworks(baseNode, nodeList, pos = cc.v2(0, 0), side = cc.v2(0, 100), range = 50, number = 100) { export async function showFireworks(baseNode, nodeList, pos = cc.v2(0, 0), side = cc.v2(0, 100), range = 50, number = 100, func) {
new Array(number).fill(' ').forEach(async (_, i) => { new Array(number).fill(' ').forEach(async (_, i) => {
let rabbonNode = new cc.Node(); let rabbonNode = new cc.Node();
...@@ -165,7 +166,7 @@ export async function showFireworks(baseNode, nodeList, pos = cc.v2(0, 0), side ...@@ -165,7 +166,7 @@ export async function showFireworks(baseNode, nodeList, pos = cc.v2(0, 0), side
rabbonFall(rabbonNode); rabbonFall(rabbonNode);
await asyncDelay(Math.random()); await asyncDelay(Math.random(), func);
cc.tween(node) cc.tween(node)
.by(0.15, { x: -10, angle: -10 }) .by(0.15, { x: -10, angle: -10 })
.by(0.3, { x: 20, angle: 20 }) .by(0.3, { x: 20, angle: 20 })
......
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