Hi, Im currently working on this sketch. Overall, whenever i overlap the mouse on any ellipse, i get a color fill and a piece of sound that start playing. The problem was the sound started looping insanely fast (partly because it was on void draw, i guess) so I use the millis() function to avoid that former issue but now i have a delay on the sound when overlaping from one ellipse to another. That being said, what i really want is to whenever the mouse overlaps the ellipse, both the sound and color fill happens only once and the same thing for every ellipse. I believe i have to put that code on a event but im not sure.
Particle p1;
Particle p2;
Particle p3;
Particle p4;
Particle p5;
Particle p6;
Particle p7;
int lastTimeCheck;
int timeIntervalFlag = 1000;
import ddf.minim.*;
Minim minim;
AudioSample sound1;
AudioSample sound2;
AudioSample sound3;
AudioSample sound4;
AudioSample sound5;
AudioSample sound6;
import spout.*;
Spout spout;
void setup() {
size(600,400, P2D);
p1 = new Particle(100,100,10);//mouse
p2 = new Particle(120,100,50);
p3 = new Particle(120,250,50);
p4 = new Particle(400,250,50);
p5 = new Particle(400,100,50);
p6 = new Particle(255,100,50);
p7 = new Particle(255,250,50);
lastTimeCheck = millis();
minim = new Minim(this);
sound1 = minim.loadSample("do.wav", 512);
sound2 = minim.loadSample("re.wav", 512);
sound3 = minim.loadSample("mi.wav", 512);
sound4 = minim.loadSample("fa.wav", 512);
sound5 = minim.loadSample("sol.wav", 512);
sound6 = minim.loadSample("la.wav", 512);
spout = new Spout(this);
spout.createSender("Spout Processing");
}
void draw() {
background(0);
if (p1.overlaps(p2)) {
p2.displayP2();
if ( millis() > lastTimeCheck + timeIntervalFlag ) {
lastTimeCheck = millis();
sound1.trigger();
}
}
if (p1.overlaps(p3)) {
p3.displayP3();
if ( millis() > lastTimeCheck + timeIntervalFlag ) {
lastTimeCheck = millis();
sound2.trigger();
}
}
if (p1.overlaps(p4)) {
p4.displayP4();
if ( millis() > lastTimeCheck + timeIntervalFlag ) {
lastTimeCheck = millis();
sound3.trigger();
}
}
if (p1.overlaps(p5)) {
p5.displayP5();
if ( millis() > lastTimeCheck + timeIntervalFlag ) {
lastTimeCheck = millis();
sound4.trigger();
}
}
if (p1.overlaps(p6)) {
p6.displayP6();
if ( millis() > lastTimeCheck + timeIntervalFlag ) {
lastTimeCheck = millis();
sound5.trigger();
}
}
if (p1.overlaps(p7)) {
p7.displayP7();
if ( millis() > lastTimeCheck + timeIntervalFlag ) {
lastTimeCheck = millis();
sound6.trigger();
}
}
p1.x = mouseX;
p1.y = mouseY;
p1.display(); //colocar marco de demas pelotas
spout.sendTexture();
}
Here goes my class
class Particle {
float x;
float y;
float r;
Particle (float tempX, float tempY, float tempR) {
x = tempX;
y = tempY;
r = tempR;
}
boolean overlaps (Particle other) {
float d = dist(x, y, other.x, other.y);
if (d < r + other.r) {
return true;
} else {
return false;
}
}
void display() {
noStroke();
noFill();
ellipse(x, y, r*2, r*2);
stroke(255);
ellipse(120,100,100,100);//izq
ellipse(120,250,100,100);//izq
ellipse(400,250,100,100);//der
ellipse(400,100,100,100);//der
ellipse(255,100,100,100);//en medio
ellipse(255,250,100,100);//en medio
}
void displayP2() {
stroke(255);
fill(0,150,0);
ellipse(x, y, r*2, r*2);
}
void displayP3() {
stroke(255);
fill(150,0,0);
ellipse(x, y, r*2, r*2);
}
void displayP4() {
stroke(255);
fill(0,0,150);
ellipse(x, y, r*2, r*2);
}
void displayP5() {
stroke(255);
fill(100,0,150);
ellipse(x, y, r*2, r*2);
}
void displayP6() {
stroke(255);
fill(150,50,100);
ellipse(x, y, r*2, r*2);
}
void displayP7() {
stroke(255);
fill(100,150,50);
ellipse(x, y, r*2, r*2);
}
}