So I have a p5js code that plays two diffrent audios when you click on two diffrent circles: let sound1, sound2; function preload() { sound1 = loadSound('Monster.mp3'); sound2 = loadSound('Blink.mp3'); } function setup() { createCanvas(400, 400); } function draw() { background(220); fill(0, 0, 255); ellipse(width/2, height/2, 100); fill(255, 0, 0); ellipse(width/4, height/4, 50); } function mouseClicked() { if (dist(mouseX, mouseY, width/2, height/2) < 50) {  sound1.play(); } if (dist(mouseX, mouseY, width/4, height/4) < 25) {  sound2.play(); } }  And it works fine on its own. However, when I add it in my projects code with other aspects, the error, TypeError: undefined is not an object (evaluating 'sound1.play') and TypeError: undefined is not an object (evaluating 'sound2.play'), always pops up.  Here it is, included in my projects code: let song; let img; var img2; var img3; var Frog, Herbs, Monster, Produce, Witch, Cauldron; //oval potion var x = 540; var y = 110; var w = 30; var h = 70; var radius = 20; //sound effects let sound1, sound2; function preload() {   sound1 = loadSound('Monster.mp3');   sound2 = loadSound('Blink.mp3'); } function preload (){   img = loadImage('Door.jpg');       img3 = loadImage('Window.PNG');      Witch = createImg('Witch.gif','');      Cauldron = createImg('Cauldron.gif', '');      Herbs = createImg('Herbs.gif', '');      Monster = createImg('Monster.gif','');     Produce = createImg('Produce.gif','');      Frog = createImg('Frog.gif','');     img2 = loadImage('Wand.jpg'); } function setup() {   createCanvas(700, 400);       //background music   song = createAudio('Music.mp3');   song.autoplay(true);   song.loop();   song.volume(0.2);      //se    } function draw() {   background(108, 93, 81);   strokeWeight(6);   //carpet   {push();   fill(87, 47, 59);   quad(79, 380, 152,300, 622,300, 550, 380);   pop();}      //right counter   {push();   fill(37, 23, 30);   square(620,190,80);   rect(360,150,200,80);   fill(37, 23, 30);   quad(619,270,620,190,562,151,560,230);   fill(0);   beginShape();   vertex(360,150);   vertex(360, 135);   vertex(620, 135);   vertex(700, 190);   vertex(620,190);   vertex(562,150);   endShape();   pop();}      //wall outline    {  line(620,135,620,5);   line(360,230,80,230);}      //shelfs   {push();   fill(0);   stroke(0);   line(230,100,340,100);   line(230,70,340,70);   line(230,40,340,40);   pop();}      //potions   {strokeWeight(1.5);      push();   fill(200);   rect(245,19,15,20);   pop();      push();   fill(255,127,140);   rect(268,24,20,15);   pop();      push();   fill(75);   rect(305,14,23,25);   pop();      push();   fill(255,206,108);   rect(252,54,30,15);   pop();      push();   fill(139,255,121);   rect(293,49,15,20);   pop();      push();   fill(160);   rect(240,81,23,18);   pop();      push();   fill(108,195,255);   rect(285,84,40,15);   pop();      push();   fill(225,141,78);   rect(250,15,5,5);      rect(276,20,5,5);      rect(310,10,5,5);      rect(270,50,5,5);      rect(298,45,5,5);      rect(249,77,5,5);      rect(290,80,5,5);   pop();}      //counter potions   //{ellipse(x,y,w,h);      //push()   //strokeWeight(1);   //fill(225,141,78);   //rect(532,68,16,10);   //pop();}      //hangers   {push();   stroke(0);   fill(0);   ellipse(250,120,10,10)   ellipse(285,120,10,10);   ellipse(320,120,10,10);   pop();}      //left counter   {push();   fill(37, 23, 30);   quad(0,365,80,260,80,151,0,255);   fill(0);   beginShape();   vertex(0,255);   vertex(77, 150);   vertex(0, 150);   endShape();   pop();}      //menu   {push();   fill(117, 133, 101);   rect(20,20,60,100);   pop();}      //window   image(img3,400,30);      //witch   Witch.position(350,60);      //cauldron   Cauldron.position(265,218);      //frog   Frog.position(25,310);      //monster   Monster.position(5,130);      //door   image(img,92,13,125,220);      //produce   {push();   strokeWeight(1);   fill(255);   rect(527,230,23,20);   pop();      Produce.position(465,155);}      //herbs   Herbs.position(600,90);   //wand cursor   image(img2,mouseX,mouseY,15,50);      //sound effects   fill(0, 0, 255);   ellipse(width/2, height/2, 100);   fill(255, 0, 0);   ellipse(width/4, height/4, 50);   }   function mouseClicked() {   if (dist(mouseX, mouseY, width/2, height/2) < 50) {     sound1.play();   }   if (dist(mouseX, mouseY, width/4, height/4) < 25) {     sound2.play();   } } It always points out sound1.play and sound2.play as undefined, how can I fix this?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

So I have a p5js code that plays two diffrent audios when you click on two diffrent circles:

let sound1, sound2;

function preload() {

sound1 = loadSound('Monster.mp3');

sound2 = loadSound('Blink.mp3');

}

function setup() {

createCanvas(400, 400);

}

function draw() {

background(220);

fill(0, 0, 255);

ellipse(width/2, height/2, 100);

fill(255, 0, 0);

ellipse(width/4, height/4, 50);

}

function mouseClicked() {

if (dist(mouseX, mouseY, width/2, height/2) < 50) {

 sound1.play();

}

if (dist(mouseX, mouseY, width/4, height/4) < 25) {

 sound2.play();

}

And it works fine on its own. However, when I add it in my projects code with other aspects, the error, TypeError: undefined is not an object (evaluating 'sound1.play') and TypeError: undefined is not an object (evaluating 'sound2.play'), always pops up. 

Here it is, included in my projects code:

let song;

let img;

var img2;
var img3;

var Frog, Herbs, Monster, Produce, Witch, Cauldron;

//oval potion
var x = 540;
var y = 110;
var w = 30;
var h = 70;
var radius = 20;

//sound effects
let sound1, sound2;

function preload() {
  sound1 = loadSound('Monster.mp3');
  sound2 = loadSound('Blink.mp3');
}

function preload (){
  img = loadImage('Door.jpg');
   
  img3 = loadImage('Window.PNG');
  
  Witch = createImg('Witch.gif','');
  
  Cauldron = createImg('Cauldron.gif', '');
  
  Herbs = createImg('Herbs.gif', '');
  
  Monster = createImg('Monster.gif','');
  
 Produce = createImg('Produce.gif','');
  
  Frog = createImg('Frog.gif','');
  
 img2 = loadImage('Wand.jpg');
}

function setup() {
  createCanvas(700, 400); 
  
  //background music
  song = createAudio('Music.mp3');
  song.autoplay(true);
  song.loop();
  song.volume(0.2);
  
  //se
  
}

function draw() {
  background(108, 93, 81);
  strokeWeight(6);

  //carpet
  {push();
  fill(87, 47, 59);
  quad(79, 380, 152,300, 622,300, 550, 380);
  pop();}
  
  //right counter
  {push();
  fill(37, 23, 30);
  square(620,190,80);
  rect(360,150,200,80);
  fill(37, 23, 30);
  quad(619,270,620,190,562,151,560,230);
  fill(0);
  beginShape();
  vertex(360,150);
  vertex(360, 135);
  vertex(620, 135);
  vertex(700, 190);
  vertex(620,190);
  vertex(562,150);
  endShape();
  pop();}
  
  //wall outline 
  {  line(620,135,620,5);
  line(360,230,80,230);}
  
  //shelfs
  {push();
  fill(0);
  stroke(0);
  line(230,100,340,100);
  line(230,70,340,70);
  line(230,40,340,40);
  pop();}
  
  //potions
  {strokeWeight(1.5);
  
  push();
  fill(200);
  rect(245,19,15,20);
  pop();
  
  push();
  fill(255,127,140);
  rect(268,24,20,15);
  pop();
  
  push();
  fill(75);
  rect(305,14,23,25);
  pop();
  
  push();
  fill(255,206,108);
  rect(252,54,30,15);
  pop();
  
  push();
  fill(139,255,121);
  rect(293,49,15,20);
  pop();
  
  push();
  fill(160);
  rect(240,81,23,18);
  pop();
  
  push();
  fill(108,195,255);
  rect(285,84,40,15);
  pop();
  
  push();
  fill(225,141,78);
  rect(250,15,5,5);
  
  rect(276,20,5,5);
  
  rect(310,10,5,5);
  
  rect(270,50,5,5);
  
  rect(298,45,5,5);
  
  rect(249,77,5,5);
  
  rect(290,80,5,5);
  pop();}
  
  //counter potions
  //{ellipse(x,y,w,h);
  
  //push()
  //strokeWeight(1);
  //fill(225,141,78);
  //rect(532,68,16,10);
  //pop();}
  
  //hangers
  {push();
  stroke(0);
  fill(0);
  ellipse(250,120,10,10)
  ellipse(285,120,10,10);
  ellipse(320,120,10,10);
  pop();}
  
  //left counter
  {push();
  fill(37, 23, 30);
  quad(0,365,80,260,80,151,0,255);
  fill(0);
  beginShape();
  vertex(0,255);
  vertex(77, 150);
  vertex(0, 150);
  endShape();
  pop();}
  
  //menu
  {push();
  fill(117, 133, 101);
  rect(20,20,60,100);
  pop();}
  
  //window
  image(img3,400,30);
  
  //witch
  Witch.position(350,60);
  
  //cauldron
  Cauldron.position(265,218);
  
  //frog
  Frog.position(25,310);
  
  //monster
  Monster.position(5,130);
  
  //door
  image(img,92,13,125,220);
  
  //produce
  {push();
  strokeWeight(1);
  fill(255);
  rect(527,230,23,20);
  pop();
  
  Produce.position(465,155);}
  
  //herbs
  Herbs.position(600,90);

  //wand cursor
  image(img2,mouseX,mouseY,15,50);
  
  //sound effects
  fill(0, 0, 255);
  ellipse(width/2, height/2, 100);
  fill(255, 0, 0);
  ellipse(width/4, height/4, 50);
  }

 

function mouseClicked() {
  if (dist(mouseX, mouseY, width/2, height/2) < 50) {
    sound1.play();
  }
  if (dist(mouseX, mouseY, width/4, height/4) < 25) {
    sound2.play();
  }
}

It always points out sound1.play and sound2.play as undefined, how can I fix this?

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Types of Loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education