why is my code playing the sound twice when it collides with the edge?
in
Core Library Questions
•
2 years ago
Hi guys. I am wondering why my code sounds like it is playing the sound twice when it collides with the edge.
to hear what i mean go to this link and you can hear the sound(
http://www.openprocessing.org/visuals/?visualID=27190)
Main:
import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
Minim minim;
int maxBalls = 11;
Ball[] ballArray;
int ballCount; // No of balls being created
AudioPlayer[] notes = new AudioPlayer[11];
//Background colour variables
float a = 0;
float b = 0;
float c = 0;
float d = 102;
float e = 204;
float f = 255;
int replace = 0;
//controling variables
int edge = 56;
int radius = 0;
int radicont = 1;
int as = 0;
int ID = 1;
int click = 0;
void setup() {
size(750, 750);
smooth();
// Create an array of balls
ballArray = new Ball[maxBalls];
//audio note creation
minim = new Minim(this);
notes[1] = minim.loadFile("1note1.wav");
notes[2] = minim.loadFile("1note2.wav");
notes[3] = minim.loadFile("1note3.wav");
notes[4] = minim.loadFile("1note4.wav");
notes[5] = minim.loadFile("note5.wav");
notes[6] = minim.loadFile("note6.wav");
notes[7] = minim.loadFile("note7.wav");
notes[8] = minim.loadFile("note8.wav");
notes[9] = minim.loadFile("note9.wav");
notes[10] = minim.loadFile("note10.wav");
}
void background1()
{
background(color(d+a,e+b,f+c));
//Change the background colours
a = (random(-1,1)) + a;
b = (random(-1,1)) + b;
c = (random(-1,1)) + c;
//black border
fill(0);
rect(0,0,edge,height);
rect(0,0,width,edge);
rect(0,height-edge,width,height);
rect(width-edge,0,width,height);
noFill();
}
void draw() {
background1();
// Display the balls and update the positions
for (int i = 0; i < ballCount; i = i + 1) {
ballArray[i].display();
ballArray[i].update();
ballArray[i].mouse();
ballArray[i].sound();
}
}
//Ball creation
void mousePressed() {
if (mouseButton == LEFT && replace == 0) {
if (as < maxBalls) {
ballArray[as] = new Ball(mouseX, mouseY, random(-2, 2), random(-2, 2), 10.0, ID);
ballCount++;
as++;
ID++;
if(as == maxBalls-1) {
replace = 1;
as = 0;
}
if (ID == maxBalls) {
ID = 1;
}
}
}
//Ball cycle
if (mouseButton == LEFT && replace == 1) {
if (mouseButton == LEFT && click == 1) {
click = 2;
}
if (mouseButton == LEFT && click == 0) {
click = 1;
}
if (ballCount < maxBalls && click == 2) {
ballArray[as] = new Ball(mouseX, mouseY, random(-2, 2), random(-2, 2), 10.0, ID);
as++;
ID++;
if(as == maxBalls - 1) {
as=0;
}
if (ID == maxBalls) {
ID = 1;
}
}
}
}
Ball class:
class Ball {
float x, y; // Position of the ball
float speedX, speedY; // Speed of the ball
float radius; // Radius of the ball
color ball_color = color(0,random(0,250)); // Color of the ball
int resizeControl = 0;
int id;
int play = 0;
// Constructor
Ball(float x_, float y_, float speedX_, float speedY_, float radius_, int id_) {
x = x_;
y = y_ ;
speedX = speedX_;
speedY = speedY_;
radius = radius_;
id = id_;
}
// Draws the ball
void display() {
fill(ball_color);
stroke(ball_color);
ellipse(x, y, radius * 2, radius * 2);
}
// Updates the ball's position, and reverses speed if it hits the wall
void update() {
x = x + speedX* mouseY/100;
y = y + speedY* mouseY/100;
// change direction when it has a wall collision
if (x + edge + radius > width || x - edge - radius < 0) {
speedX = - speedX;
}
if (y + edge + radius > height || y - edge - radius < 0) {
speedY = - speedY;
}
}
void mouse() {
int CO = 10;
if (mousePressed) {
resizeControl = 1;
if(mouseButton == RIGHT) {
if (resizeControl == 1) {
if(radius<50 && radicont==1) {
radius=radius+1;
notes[1].setGain(0);
notes[2].setGain(0);
notes[3].setGain(0);
notes[4].setGain(0);
notes[5].setGain(0);
notes[6].setGain(0);
notes[7].setGain(0);
notes[8].setGain(0);
notes[9].setGain(0);
notes[10].setGain(0);
if(radius==49) {
radicont=0;
}
}
if(radius>10 && radicont==0) {
radius=radius-1;
notes[1].setGain(0);
notes[2].setGain(0);
notes[3].setGain(0);
notes[4].setGain(0);
notes[5].setGain(0);
notes[6].setGain(0);
notes[7].setGain(0);
notes[8].setGain(0);
notes[9].setGain(0);
notes[10].setGain(0);
if(radius==11) {
radicont=1;
}
}
}
}
else if (mouseButton == LEFT) {
resizeControl = 0;
}
}
}
//play sounds when wall collision is present
void sound() {
if (x + edge + radius > width || x - edge - radius < 0) {
play = 1;
notes[1].rewind();
}
if (play == 1) {
notes[id].play(0);
notes[id].rewind();
play = 0;
}
// if (y + edge + radius > height || y - edge - radius < 0) {
// notes[id].loop(0);
//}
}
}
thanks for your help.
1