function scene1() { background(0); image(img1, width / 2, height / 2);//场景背景1…
- User
function scene1() { background(0); image(img1, width / 2, height / 2);//场景背景1 //添加声音传感器的检测 if (millis() - soundTime > 5000 && !text2Displayed && soundDetected) { //停顿五秒后显示提示信息 text(text2, width / 2, height * 0.8); text2Displayed = true; soundDetected = false; //重置声音检测变量,等待下一次声音输入 }
//场景1的其他元素
}
//声音检测函数 function detectSound() { if (getAudioContext().state !== 'running') { getAudioContext().resume(); } soundDetected = true; soundTime = millis(); }
//添加声音检测事件 function mouseClicked() { showImg8 = !showImg8; }
//添加声音检测事件 function mousePressed() { if (T1 === -1) { textIndex = 0; textLoading = true; T1 = 420; } else if (T1 === 0) { T2 = 420; } else if (T2 === -1){ T3 = 540; } else { detectSound(); } }
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 = 540; } } if (T4 > 0){ T4--; } //对话框出现与否 function mouseClicked() { showImg8 = !showImg8;// 每次点击开关状态取反 } //字幕出现的时长 function mousePressed(){ if (T1 === -1) { textIndex = 0; textLoading = true; T1 = 420; } else if (T1 === 0) { T2 = 420; } else if (T2 === -1){ T3 = 540; } }
帮我修改完善这段代码,声音传感器要放在第一段话之后
- Assistant
如下是修改后的代码:
let img1, img2, img3, img4, img5, img6, img7, img8; let zoulu = [], zoulu2 = []; let cRen = {x: 100, y: 430}; let vRen = {x: 20, y: 0}; let vRen2 = {x: 20, y: -5}; let img7X, img7Y; let img7FrameIndex = 0; let showImage1 = true, showImage2 = false; let isMoving = false; let soundDetected = false; let soundTime = 0; let text2Displayed = false; let showImage3 = true; let currentText = "请按时参加会议"; let showImage4 = false; let showImg8 = false; let T1 = -1, T2 = -1, T3 = -1, T4 = -1;
//对话框文字 let dialogue1 = "我们已经迟到了,快点!"; let dialogue2 = "这个会议很重要,得参加!"; let dialogue3 = "我们要为公司的明天努力奋斗!";
//提示文字 let text1 = "按 ← 或 → 键控制小人移动"; let text2 = "哎呀,会议迟到了!"; let text3 = "请按时参加会议";
let textIndex = 0; let textLoading = true;
function preload() { img1 = loadImage('https://iceland101.oss-cn-beijing.aliyuncs.com/P5JS/Scene1-background.png'); img2 = loadImage('https://iceland101.oss-cn-beijing.aliyuncs.com/P5JS/Scene4-background.png'); img3 = loadImage('https://iceland101.oss-cn-beijing.aliyuncs.com/P5JS/Scene5-background.png'); img4 = loadImage('https://iceland101.oss-cn-beijing.aliyuncs.com/P5JS/Scene6-background.png'); img5 = loadImage('https://iceland101.oss-cn-beijing.aliyuncs.com/P5JS/Scene7-background.png'); img6 = loadImage('https://iceland101.oss-cn-beijing.aliyuncs.com/P5JS/Scene8-background.png'); img7 = loadImage('https://iceland101.oss-cn-beijing.aliyuncs.com/P5JS/zoulu.png'); img8 = loadImage('https://iceland101.oss-cn-beijing.aliyuncs.com/P5JS/minion-big.png'); for (let i = 0; i < 9; i++) { zoulu[i] = loadImage(
https://iceland101.oss-cn-beijing.aliyuncs.com/P5JS/zoulu-${i}.png); } for (let i = 0; i < 6; i++) { zoulu2[i] = loadImage(https://iceland101.oss-cn-beijing.aliyuncs.com/P5JS/zoulu2-${i}.png); } }function setup() { createCanvas(1920, 1080); }
function draw() { if (showImage1) { scene1(); } else if (showImage2) { scene2(); } else if (showImage3) { scene3(); } else if (showImage4) { scene4(); } else { scene5(); } }
function scene1() { background(0); image(img1, width / 2, height / 2);//场景背景1
//场景1的其他元素
//添加声音传感器的检测 if (millis() - soundTime > 5000 && !text2Displayed && soundDetected) { //停顿五秒后显示提示信息 text(text2, width / 2, height * 0.8); text2Displayed = true; soundDetected = false; //重置声音检测变量,等待下一次声音输入 } }
function detectSound() { if (getAudioContext().state !== 'running') { getAudioContext().resume(); } soundDetected = true; soundTime = millis(); }
function mouseClicked() { showImg8 = !showImg8; }
function mousePressed() { if (millis() - soundTime > 1000) { detectSound(); } if (T1 === -1) { textIndex = 0; textLoading = true; T1 = 420; } else if (T1 === 0) { T2 = 420; } else if (T2 === -1) { T3 = 540; } }
function keyPressed() { if (keyCode === LEFT_ARROW) { isMoving = true; showImage1 = true; showImage2 = false; vRen.x = -20; } else if (keyCode === RIGHT_ARROW) { isMoving = true; showImage1 = true; showImage2 = false; vRen.x = 20; } else if (key === '.') { isMoving = false; vRen.x = 0; img7FrameIndex = 0; } else if (key === 'a') { isMoving = true; showImage1 = false; showImage2 = true; vRen2.x = -20; } else if (key === 'd') { isMoving = true; showImage1 = false; showImage2 = true; vRen2.x = 20; } else if (key === 's') { isMoving = false; vRen2.x = 0; img7FrameIndex = 0; } else if (key === '1') { showImage3 = true; showImage4 = false; } else if (key === '2') { showImage3 = false; showImage4 = true; } else if (key === '3') { showImage1 = false; showImage2 = false; showImage3 = false; showImage4 = false; } }
function animateZoulu() { if (frameCount % 5 === 0) { img7FrameIndex = (img7FrameIndex + 1) % 9; } }
function animateZoulu2() { if (frameCount % 5 === 0) { img7FrameIndex = (img7FrameIndex + 1) % 6; } }
function scene2() { image(img2, width / 2, height / 2); }
function scene3() { background(255); text(currentText, width / 2, height / 2); }
function scene4() { image(img4, width / 2, height / 2); text(text1, width / 2, 50); }
function scene5() { background(255); image(img5, width / 2, height / 2); text(text3, 1720, 350); }
//对白字幕 function displayDialogue() { let currentDialogue = ''; if (T1 === 0) { currentDialogue = dialogue1; T2 = 420; } else if (T2 === 0) { currentDialogue = dialogue2; T3 = 540; } else if (T3 === 0) { currentDialogue = dialogue3; } if (currentDialogue !== '') { textAlign(CENTER); fill(255); textFont("Simsun"); textSize(35); text(currentDialogue, width * 0.5, height * 0.7); } }
//字幕出现与否 function displaySubtitle() { if (textLoading && textIndex < text1.length) { textAlign(CENTER); fill(255); textFont("Simsun"); textSize(35); text(text1.substring(0, textIndex + 1), width * 0.5, 50); if (frameCount % 4 === 0) {//每4帧更新一次 textIndex++; } } else { textLoading = false; } }
function displayImage() { 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); } else if (showImage4) { image(img2, width / 2, height / 2); if (showImg8) { image(img8, width / 2, height / 2); } text(text2, width / 2, height * 0.8);//放大图,和缩小提示词 } }
function displayHint() { if (showImage3) { displaySubtitle(); } else { textAlign(CENTER); fill(255); textFont("Simsun"); textSize(35); text(text3, 1720, 350); } }
function displayDialogBox() { if (T1 !== -1 || T4 > 0) { fill(0, 150); noStroke(); rect(0, height * 0.6, width, height * 0.4); if (T4 === 0) { T1 = -1; } } }
function display() { displayHint(); displayImage(); displayDialogBox(); displayDialogue(); }
function draw() { if (showImage1 || showImage2) { background(255); } else if (showImage3) { background(0); } else { background(255); } display(); }