Commit 5fd800fc authored by 范雪寒's avatar 范雪寒

fix: 结束音频与热区读音冲突问题

parent b0d27c7b
import { onHomeworkFinish, playAudioByUrl, playDragonBoneAnimation } from "../script/util"; import { asyncDelay, onHomeworkFinish, playDragonBoneAnimation } from "../script/util";
import { defaultData } from "../script/defaultData"; import { defaultData } from "../script/defaultData";
cc.Class({ cc.Class({
...@@ -140,49 +140,56 @@ cc.Class({ ...@@ -140,49 +140,56 @@ cc.Class({
}, },
initView() { async initView() {
this.coolCatSpeakStart(() => {
this.coolCatSpeakStart();
});
const canvas = cc.find('Canvas');
const bg = cc.find('Canvas/bg/ImgBg'); const bg = cc.find('Canvas/bg/ImgBg');
this.loadSpriteByUrl(bg, this.data.bgItem.url, () => { await this.asyncLoadSpriteByUrl(bg, this.data.bgItem.url);
bg.scale = Math.min((canvas.width / bg.width), (canvas.height / bg.height));
const canvas = cc.find('Canvas');
this.itemList = []; bg.scale = Math.min((canvas.width / bg.width), (canvas.height / bg.height));
this.data.hotZoneItemArr.forEach(async itemData =>{
const item = await this.createItem(itemData, bg); this.itemList = [];
this.itemList.push(item); this.data.hotZoneItemArr.forEach(async itemData => {
item.on('click', () => { const item = await this.createItem(itemData, bg);
item.clicked = true; this.itemList.push(item);
if (this.itemList.every(tmpItem => tmpItem.clicked)) {
this.coolCatSpeakEnd();
}
playAudioByUrl(itemData.audio_url);
const time = 0.05;
cc.tween(item)
.to(time, { angle: 10 })
.to(time * 2, { angle: -10 })
.to(time, { angle: 0 })
.start();
});
});
}); });
}, },
createItem(itemData, bg) { shakeItem(item) {
return new Promise((resolve, reject) => { const time = 0.05;
const bgScale = bg.width / this.data.bgItem.rect.width; cc.tween(item)
.to(time, { angle: 10 })
const item = cc.instantiate(cc.find('item')); .to(time * 2, { angle: -10 })
item.parent = bg; .to(time, { angle: 0 })
this.loadSpriteByUrl(item, itemData.pic_url, () => { .start();
item.scale = itemData.imgScale * bgScale; },
item.x = itemData.rect.x * bgScale - bg.width / 2 + item.width * item.scaleX / 2;
item.y = bg.height / 2 - itemData.rect.y * bgScale - item.height * item.scaleY / 2; async createItem(itemData, bg) {
resolve(item); const bgScale = bg.width / this.data.bgItem.rect.width;
});
const item = cc.instantiate(cc.find('item'));
item.parent = bg;
item.on('click', async () => {
item.clicked = true;
this.shakeItem(item);
await this.asyncPlayAudioByUrl(itemData.audio_url);
if (this.itemList.every(tmpItem => tmpItem.clicked)) {
await asyncDelay(0.3);
await this.coolCatSpeakEnd();
onHomeworkFinish();
}
}); });
await this.asyncLoadSpriteByUrl(item, itemData.pic_url);
item.scale = itemData.imgScale * bgScale;
item.x = itemData.rect.x * bgScale - bg.width / 2 + item.width * item.scaleX / 2;
item.y = bg.height / 2 - itemData.rect.y * bgScale - item.height * item.scaleY / 2;
return item;
}, },
initListener() { initListener() {
...@@ -190,26 +197,18 @@ cc.Class({ ...@@ -190,26 +197,18 @@ cc.Class({
}, },
coolCatSpeakStart(cb) { async coolCatSpeakStart(cb) {
const cat = cc.find('Canvas/catFrame/cat'); const cat = cc.find('Canvas/catFrame/cat');
playDragonBoneAnimation(cat, 'begin', -1); playDragonBoneAnimation(cat, 'begin', -1);
this.playAudioByUrl(this.data.startAudio, () => { await this.asyncPlayAudioByUrl(this.data.startAudio);
playDragonBoneAnimation(cat, 'normal', -1); playDragonBoneAnimation(cat, 'normal', -1);
if (cb) {
cb();
}
});
}, },
coolCatSpeakEnd(cb) { async coolCatSpeakEnd() {
const cat = cc.find('Canvas/catFrame/cat'); const cat = cc.find('Canvas/catFrame/cat');
playDragonBoneAnimation(cat, 'finish', -1); playDragonBoneAnimation(cat, 'finish', -1);
this.playAudioByUrl(this.data.endAudio, () => { await this.asyncPlayAudioByUrl(this.data.endAudio);
playDragonBoneAnimation(cat, 'normal', -1); playDragonBoneAnimation(cat, 'normal', -1);
if (cb) {
cb();
}
});
}, },
...@@ -233,27 +232,37 @@ cc.Class({ ...@@ -233,27 +232,37 @@ cc.Class({
loadSpriteByUrl(node, url, cb) { asyncLoadSpriteByUrl(node, url) {
cc.loader.load({ url }, (err, img) => { return new Promise((resolve, reject) => {
const spriteFrame = new cc.SpriteFrame(img) cc.loader.load({ url }, (err, img) => {
const spr = node.getComponent(cc.Sprite); const spriteFrame = new cc.SpriteFrame(img)
spr.spriteFrame = spriteFrame; const spr = node.getComponent(cc.Sprite);
if (cb) { spr.spriteFrame = spriteFrame;
cb(); resolve();
} });
}); });
}, },
playAudioByUrl(audio_url, cb = null) { currentAudioId: null,
if (audio_url) { stopCurrentAudio() {
if (this.currentAudioId !== null) {
cc.audioEngine.stop(this.currentAudioId);
this.currentAudioId = null;
}
},
asyncPlayAudioByUrl(audio_url) {
if (!audio_url) {
return;
}
return new Promise((resolve, reject) => {
cc.assetManager.loadRemote(audio_url, (err, audioClip) => { cc.assetManager.loadRemote(audio_url, (err, audioClip) => {
this.stopCurrentAudio();
const audioId = cc.audioEngine.play(audioClip, false, 0.8); const audioId = cc.audioEngine.play(audioClip, false, 0.8);
if (cb) { cc.audioEngine.setFinishCallback(audioId, () => {
cc.audioEngine.setFinishCallback(audioId, () => { resolve();
cb(); });
});
}
}); });
} });
}, },
}); });
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