I want to make a function for.
if I click the correct answer, that will make a sound
I tried it, but it isn’t working
I don’t know what is wrong… can anybody help me?
import processing.sound.*;
SoundFile file;
String[] Texts = {
“Question A”,
“Question B”,
“Question C”,
“Question D”,
“Question E”,
“Question F”,
“Question G”
};
String[] RightAnswer = {
“Yes”,
“No”,
“Yes”,
“No”,
“No”,
“Yes”,
“No”
};
int n = int (random(0, 6));
//String myText;
int totalWrong = 0;
int totalCorrect = 0;
//int question = 0;
//String correctAnswer;
//String WrongAnswer;
//int True= 0;
//int False= 0;
void setup() {
size(800, 400);
background(255);
smooth();
ellipseMode(CENTER);
file = new SoundFile(this, “bell.wav”);
file.play();
}
void draw() {
// draw() runs 60 times per second again and again
// we clear the screen to WHITE
background(255);
// we show stuff
noStroke();
//Button 1
fill(255, 20, 40);
if (button1())
fill(0, 255, 0);
ellipse(320, 100, 20, 20);
text(“YES”, 320+22, 100);
//Button 2
fill(255, 20, 40);
if (button2())
fill(0, 255, 0);
ellipse(420, 100, 20, 20);
text(“NO”, 420+22, 100);
fill(0);
rect(width/2, height/2, 300, 100);
textSize(20);
text(Texts[n], 10, 30);
text(RightAnswer[n], 10, 30+22); // for testing what happens!!!
fill(255);
fill(0);
text("", 0, 300, 800, 700);
//text(question, 20, 80);
text("Correct? ", 20+110+55, 80+30);
text(“Correct:”, 600, 40);
text(totalCorrect, 620, 80);
text(“Incorrect:”, 600, 140);
text(totalWrong, 620, 180);
}
void mousePressed() {
// CHECK Buttons
if (button1()) {
// button 1: Yes
println(“button1”);
if (RightAnswer[n].equals(“Yes”)) {
totalCorrect++;
} else {
totalWrong++;
}
goOn();
} else if (button2()) {
// button 2: No
println(“button2”);
if (RightAnswer[n].equals(“No”)) {
totalCorrect++;
} else {
totalWrong++;
}
goOn();
}//else if
}
void goOn() {
n = int (random(0, 6));
}
boolean button1() {
return
dist(mouseX, mouseY, 320, 100) < 70;
}
boolean button2() {
return
dist(mouseX, mouseY, 480, 100) < 70;
}
//