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

feat: setTimeout

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