let dialogue1 ="哈喽,还记得你的童年是什么样子的嘛?" let dialogue2 = "童年所经历的一切,都会在时光里发酵成甜蜜的回忆。" let…
- User
let dialogue1 ="哈喽,还记得你的童年是什么样子的嘛?" let dialogue2 = "童年所经历的一切,都会在时光里发酵成甜蜜的回忆。" let dialogue3 = "几岁时的一棵树一株草一片天一条街,或许就是你心灵的栖息地和灵魂的中转站。" let dialogue4 = "希望你的一生都可以被童年治愈。" let dialogue5 = "那么请你来说几个可以准确描述你童年记忆的关键词吧~" let dialogue6 = "通关成功,接下来我们进入下一个主题房间。" let sound1, sound2, sound3, let T1 = -1; let T2 = -1; let T3 = 0; let T4 = 0; let T5 = 0; let T6 = 0;
let showImgClick = false; // 点击开关 let showSecondText = true; // 新增变量控制第二段文字是否显示过
function preload() { sound1 = loadSound('1.m4a'); sound2 = loadSound('2.m4a'); sound3 = loadSound('3.m4a'); }
function scene1() { background(0); image(img1, width / 2, height / 2);//场景背景1
if (showImage1) {//左右键控制的变量,具体控制小人运动 if (isMoving) { animateZoulu(); let nextX = cRen.x + vRen.x; if (nextX >= 250 && nextX <= 1670) {//限制小人不走出画外 cRen.x = nextX; } } image(zoulu[img7FrameIndex], cRen.x, cRen.y); } else if (showImage2) { if (isMoving) { animateZoulu2(); let nextX = cRen.x + vRen2.x; if (nextX >= 250 && nextX <= 1670) { cRen.x = nextX; } } image(zoulu2[img7FrameIndex], cRen.x, cRen.y);//一直到这里,作用同上 } if (showImage3) { text(currentText, width / 2, height * 0.8);//墙上提示词,不按时 }else if(showImage4){ image(img2, width / 2, height / 2); text(text2, width / 2, height * 0.8);//放大图,和缩小提示词 } if (showImg8) { //激活显示图片的开关 image(img8, width / 2, height / 2); } img7X = cRen.x; img7Y = cRen.y;//有关小人走路的变量,不动
fill(255); text(text3, 1720, 350);//门上提示词
//对白字幕 if (T1 === 0 && showSecondText){ T2 = 420; showSecondText = false; // 第二段文字只出现一次 } else if (T1 > 0) { textFont("Simsun"); textSize(35); text(dialogue1,width * 0.5, height * 0.7); T1--; } else if (T2 > 0) { textFont("Simsun"); textSize(35); text(dialogue2, width * 0.5, height * 0.7); T2--; if (T2 === 0){ T3 = 540; } } else if (T3 > 0) { textFont("Simsun"); textSize(35); text(dialogue3, width * 0.5, height * 0.7); T3--; if (T3 === 0){ T4 = 420; } } else if (T4 > 0) { textFont("Simsun"); textSize(35); text(dialogue4, width * 0.5, height * 0.7); T4--; if (T4 === 0){ T5 = 420; } } else if (T5 > 0) { textFont("Simsun"); textSize(35); text(dialogue5, width * 0.5, height * 0.7); T5--; if (T5 === 0){ T6 = 480; } } else if (T6 >0) { textFont("Simsun"); textSize(35); text(dialogue6, width * 0.5, height * 0.7); T6--; } } //对话框出现与否 function mouseClicked() { showImg8 = !showImg8;// 每次点击开关状态取反 } //字幕出现的时长 function mousePressed(){ if (T1 === -1) { textIndex = 0; textLoading = true; T1 = 420; } if (sound1.isPlaying()){ sound1.pause(); } else{ sound1.play(); } }帮我完善这段p5.js代码,使得点击鼠标后加载第一段音频sound1后自动加载第二段音频sound2
- Assistant
可以在mousePressed函数中添加以下代码:
function mousePressed(){ if (T1 === -1) { textIndex = 0; textLoading = true; T1 = 420; } if (sound1.isPlaying()){ sound1.pause(); } else{ sound1.play(); } sound1.onended(function(){ // sound1播放完毕后自动加载sound2 if (sound2.isPlaying()){ sound2.pause(); } else{ sound2.play(); } }); }这里使用了p5.js中的onended函数,它会在当前音频播放完毕后触发,我们可以在这个函数中加载第二段音频sound2。
- User
- Assistant
在第二段音频sound2播放完后触发的函数里,再添加一段代码来加载第三段音频sound3,可以这样写:
sound1.onended(function(){ if (sound2.isPlaying()){ sound2.pause(); } else { sound2.play(); } }); sound2.onended(function(){ // sound2播放完毕后自动加载sound3 if (sound3.isPlaying()){ sound3.pause(); } else{ sound3.play(); } });这里我们使用了onended函数,当第二段音频sound2播放完后触发,再使用onended函数来自动加载第三段音频sound3。这样就可以实现目标了。