let image1; let image2; let image3; let image4; let overlayImage; let currentImageIndex…
- User
let image1; let image2; let image3; let image4; let overlayImage; let currentImageIndex = 0; let falling = false; let broken = false; let overlayY = 0; let oriPostionX=0; let oriPostionY=0;
let px= 0; let py =0; let smashSound; let mic; let flag = true; // 定义变量flag
function preload() { // 加载图片 image1 = loadImage('loudao.jpg'); //楼道 image2 = loadImage('fangjian4.jpg');//第四个房间 image3 = loadImage('datu4.jpg'); // 放大的窗户 pingzi = loadImage('pingzi.png'); // 瓶子的图片 image4 = loadImage('kaichuang.png'); smashSound = loadSound('glass_smash.mp3');
oriPostionX=windowWidth/2 +200; oriPostionY=windowHeight/2 +100; px=oriPostionX; py=oriPostionY;//这里是原先的位置,<<<<< acc=0; }
function setup() { createCanvas(windowWidth,windowHeight); mic = new p5.AudioIn(); mic.start(); }
function draw() { if(flag){
// 根据当前图片索引绘制对应的图像 if (currentImageIndex === 0) { background(0); imageMode(CENTER); image(image1, windowWidth/2, windowHeight/2); imageMode(CORNER);
} else if (currentImageIndex === 1) { background(10, 24, 37); imageMode(CENTER); image(image2, windowWidth/2, windowHeight/2); imageMode(CORNER); } else if(currentImageIndex == 2){ // 在画面中心绘制第三张图片,并将尺寸扩大1.5倍 background(10, 24, 37); imageMode(CENTER); image(image3, windowWidth/2, windowHeight/2); imageMode(CORNER); }
if(currentImageIndex ==2 && falling) { imageMode(CENTER); if(broken==false){ image(pingzi,px,py); py+=acc*0.1; acc++; //<<<<acc代表加速度 if(abs(py-windowHeight*7/8)<20){ //<<<<<<<<<<<<当py的坐标临近窗口高度7/8的时候触发事件 smashSound.play(); broken = true; } }else{ image(pingzi,px,py); } imageMode(CORNER);} }
if(currentImageIndex ==3){ flag=false; imageMode(CENTER); background(255); image(image4,windowWidth/2, windowHeight/2); imageMode(CORNER); smashSound.setVolume(1); } } function keyPressed() { if (key === 'd' || key === 'D') { currentImageIndex = (currentImageIndex + 1) % 4; // 切换图片索引
if(broken){ falling=false; broken=false; px=oriPostionX; py=oriPostionY;//这里是原先的位置,<<<<< acc=0; }} if (key === 'q' || key === 'Q') { falling = true; } }
function windowResized() { resizeCanvas(windowWidth, windowHeight); } // 在打碎玻璃后,播放照片image4 function mouseClicked() { mic.getLevel(function(level) { console.log(level); if (level > 0.05) { // 判断声音强度,如果大于0.05就播放第四张图片 currentImageIndex=3; } }); }为什么第四张照片按D键才能出现?帮我改成只能通过外部声音超过30分贝出现
- Assistant
为了实现只能通过外部声音超过30分贝触发第四张图片的显示,我们需要在mouseClicked()函数中增加一个判断条件,只有当声音强度超过30分贝时才会触发图片显示。同时,我们需要把flag的初始值设为false,这样在setup()函数中就不会执行绘制的代码,直到声音触发图片显示后才会修改flag的值使得绘制代码得以执行。以下是修改后的代码:
let image1; let image2; let image3; let image4; let overlayImage; let currentImageIndex = 0; let falling = false; let broken = false; let overlayY = 0; let oriPostionX = 0; let oriPostionY = 0;
let px = 0; let py = 0; let smashSound; let mic; let flag = false; // flag的初始值设为false
function preload() { // 加载图片 image1 = loadImage('loudao.jpg'); //楼道 image2 = loadImage('fangjian4.jpg'); //第四个房间 image3 = loadImage('datu4.jpg'); // 放大的窗户 pingzi = loadImage('pingzi.png'); // 瓶子的图片 image4 = loadImage('kaichuang.png'); smashSound = loadSound('glass_smash.mp3');
oriPostionX = windowWidth / 2 + 200; oriPostionY = windowHeight / 2 + 100; px = oriPostionX; py = oriPostionY; //这里是原先的位置,<<<<< acc = 0; }
function setup() { createCanvas(windowWidth, windowHeight); mic = new p5.AudioIn(); mic.start(); }
function draw() { if (flag) { // 根据当前图片索引绘制对应的图像 if (currentImageIndex === 0) { background(0); imageMode(CENTER); image(image1, windowWidth / 2, windowHeight / 2); imageMode(CORNER); } else if (currentImageIndex === 1) { background(10, 24, 37); imageMode(CENTER); image(image2, windowWidth / 2, windowHeight / 2); imageMode(CORNER); } else if (currentImageIndex == 2) { // 在画面中心绘制第三张图片,并将尺寸扩大1.5倍 background(10, 24, 37); imageMode(CENTER); image(image3, windowWidth / 2, windowHeight / 2); imageMode(CORNER); }
if (currentImageIndex == 2 && falling) { imageMode(CENTER); if (broken == false) { image(pingzi, px, py); py += acc * 0.1; acc++; //<<<<acc代表加速度 if (abs(py - windowHeight * 7 / 8) < 20) { //<<<<<<<<<<<<当py的坐标临近窗口高度7/8的时候触发事件 smashSound.play(); broken = true; } } else { image(pingzi, px, py); } imageMode(CORNER); }}
if (currentImageIndex == 3) { imageMode(CENTER); background(255); image(image4, windowWidth / 2, windowHeight / 2); imageMode(CORNER); smashSound.setVolume(1); } }
function keyPressed() { if (key === 'd' || key === 'D') { currentImageIndex = (currentImageIndex + 1) % 4; // 切换图片索引 if (broken) { falling = false; broken = false; px = oriPostionX; py = oriPostionY; //这里是原先的位置,<<<<< acc = 0; } } }
function windowResized() { resizeCanvas(windowWidth, windowHeight); }
// 在打碎玻璃后,播放照片image4 function mouseClicked() { mic.getLevel(function(level) { console.log(level); if (level > 0.3) { // 声音强度大于0.3才触发 flag = true; // 修改flag的值使得绘制代码得以执行 currentImageIndex = 3; } }); }
- User
let image1; let image2; let image3; let image4; let overlayImage; let currentImageIndex = 0; let falling = false; let broken = false; let overlayY = 0; let oriPostionX=0; let oriPostionY=0;
let px= 0; let py =0; let smashSound; let mic; let flag = false; //
function preload() { // 加载图片 image1 = loadImage('loudao.jpg'); //楼道 image2 = loadImage('fangjian4.jpg');//第四个房间 image3 = loadImage('datu4.jpg'); // 放大的窗户 pingzi = loadImage('pingzi.png'); // 瓶子的图片 image4 = loadImage('kaichuang.png'); smashSound = loadSound('glass_smash.mp3');
oriPostionX=windowWidth/2 +200; oriPostionY=windowHeight/2 +100; px=oriPostionX; py=oriPostionY;//这里是原先的位置,<<<<< acc=0; }
function setup() { createCanvas(windowWidth,windowHeight); mic = new p5.AudioIn(); mic.start(); }
function draw() { if(flag){
// 根据当前图片索引绘制对应的图像 if (currentImageIndex === 0) { background(0); imageMode(CENTER); image(image1, windowWidth/2, windowHeight/2); imageMode(CORNER);
} else if (currentImageIndex === 1) { background(10, 24, 37); imageMode(CENTER); image(image2, windowWidth/2, windowHeight/2); imageMode(CORNER); } else if(currentImageIndex == 2){ // 在画面中心绘制第三张图片,并将尺寸扩大1.5倍 background(10, 24, 37); imageMode(CENTER); image(image3, windowWidth/2, windowHeight/2); imageMode(CORNER); }
if(currentImageIndex ==2 && falling) { imageMode(CENTER); if(broken==false){ image(pingzi,px,py); py+=acc*0.1; acc++; //<<<<acc代表加速度 if(abs(py-windowHeight*7/8)<20){ //<<<<<<<<<<<<当py的坐标临近窗口高度7/8的时候触发事件 smashSound.play(); broken = true; } }else{ image(pingzi,px,py); } imageMode(CORNER);} }
if(currentImageIndex ==3){ imageMode(CENTER); background(255); image(image4,windowWidth/2, windowHeight/2); imageMode(CORNER); smashSound.setVolume(1); } } function keyPressed() { if (key === 'd' || key === 'D') { currentImageIndex = (currentImageIndex + 1) % 4; // 切换图片索引
if(broken){ falling=false; broken=false; px=oriPostionX; py=oriPostionY;//这里是原先的位置,<<<<< acc=0; }} if (key === 'q' || key === 'Q') { falling = true; } }
function windowResized() { resizeCanvas(windowWidth, windowHeight); } // 在打碎玻璃后,播放照片image4 function mouseClicked() { mic.getLevel(function(level) { console.log(level); if (level > 0.05) { // 判断声音强度,如果大于0.05就播放第四张图片 flag = true; // 修改flag的值使得绘制代码得以执行 currentImageIndex=3; } }); }帮我修改正确,使代码能够运行
- Assistant
以下是修改后的代码,现在应该可以正常运行了。主要的修改包括把flag的初始值设为false,以及在mouseClicked()函数中增加一个判断条件,只有当声音强度超过0.05才会触发图片显示。同时需要注意把判断声音强度部分的条件修改为小于而不是大于。
let image1; let image2; let image3; let image4; let overlayImage; let currentImageIndex = 0; let falling = false; let broken = false; let overlayY = 0; let oriPostionX = 0; let oriPostionY = 0;
let px = 0; let py = 0; let smashSound; let mic; let flag = false; // flag的初始值设为false
function preload() { // 加载图片 image1 = loadImage('loudao.jpg'); //楼道 image2 = loadImage('fangjian4.jpg'); //第四个房间 image3 = loadImage('datu4.jpg'); // 放大的窗户 pingzi = loadImage('pingzi.png'); // 瓶子的图片 image4 = loadImage('kaichuang.png'); smashSound = loadSound('glass_smash.mp3');
oriPostionX = windowWidth / 2 + 200; oriPostionY = windowHeight / 2 + 100; px = oriPostionX; py = oriPostionY; //这里是原先的位置,<<<<< acc = 0; }
function setup() { createCanvas(windowWidth, windowHeight); mic = new p5.AudioIn(); mic.start(); }
function draw() { if (flag) { // 根据当前图片索引绘制对应的图像 if (currentImageIndex === 0) { background(0); imageMode(CENTER); image(image1, windowWidth / 2, windowHeight / 2); imageMode(CORNER); } else if (currentImageIndex === 1) { background(10, 24, 37); imageMode(CENTER); image(image2, windowWidth / 2, windowHeight / 2); imageMode(CORNER); } else if (currentImageIndex == 2) { // 在画面中心绘制第三张图片,并将尺寸扩大1.5倍 background(10, 24, 37); imageMode(CENTER); image(image3, windowWidth / 2, windowHeight / 2); imageMode(CORNER); }
if (currentImageIndex == 2 && falling) { imageMode(CENTER); if (broken == false) { image(pingzi, px, py); py += acc * 0.1; acc++; //<<<<acc代表加速度 if (abs(py - windowHeight * 7 / 8) < 20) { //<<<<<<<<<<<<当py的坐标临近窗口高度7/8的时候触发事件 smashSound.play(); broken = true; } } else { image(pingzi, px, py); } imageMode(CORNER); }}
if (currentImageIndex == 3) { imageMode(CENTER); background(255); image(image4, windowWidth / 2, windowHeight / 2); imageMode(CORNER); smashSound.setVolume(1); } }
function keyPressed() { if (key === 'd' || key === 'D') { currentImageIndex = (currentImageIndex + 1) % 4; // 切换图片索引 if (broken) { falling = false; broken = false; px = oriPostionX; py = oriPostionY; //这里是原先的位置,<<<<< acc = 0; } } if (key === 'q' || key === 'Q') { falling = true; } }
function windowResized() { resizeCanvas(windowWidth, windowHeight); }
// 在打碎玻璃后,播放照片image4 function mouseClicked() { mic.getLevel(function(level) { console.log(level); if (level < 0.05) { // 声音强度小于0.05才触发 flag = true; // 修改flag的值使得绘制代码得以执行 currentImageIndex = 3; } }); }