Get a sound associated with a class object impact.
in
Core Library Questions
•
2 years ago
Hi guys.
I want to be able to make the circles play a sound when they bounce off the sides in this code.
Here is the main code
int maxBalls = 21;
Ball[] ballArray;
int ballCount; // No of balls being created
//Background colour variables
float a = 0;
float b = 0;
float c = 0;
float d = 102;
float e = 204;
float f = 255;
int replace = 0;
//controls
int edge = 56;
int radius = 0;
int radicont = 1;
void setup() {
size(750, 750);
smooth();
// Create an array of balls
ballArray = new Ball[maxBalls];
}
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();
}
}
int as = 0;
void mousePressed() {
if (mouseButton == LEFT && replace == 0) {
if (as < maxBalls) {
ballArray[as] = new Ball(mouseX, mouseY, random(-2, 2), random(-2, 2), 10.0);
ballCount++;
as++;
if(ballCount == maxBalls - 1) {
replace = 1;
as = 0;
}
}
}
if (mouseButton == LEFT && replace == 1) {
if (ballCount < maxBalls) {
//as = 0;
ballArray[as] = new Ball(mouseX, mouseY, random(-2, 2), random(-2, 2), 10.0);
// ballCount++;
as++;
if(as == maxBalls - 1) {
as=0;}
}
}
}
and here is the 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;
// Constructor
Ball(float x_, float y_, float speedX_, float speedY_, float radius_) {
x = x_;
y = y_ ;
speedX = speedX_;
speedY = speedY_;
radius = radius_;
}
// 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;
// Reverse speed if it hits the wall
if (x + edge + radius > width || x - edge - radius < 0) {
speedX = - speedX;
}
if (y + edge + radius > height || y - edge - radius < 0) {
speedY = - speedY;
}
}
void mouse() {
if (mousePressed) {
resizeControl = 1;
if(mouseButton == RIGHT) {
if (resizeControl == 1) {
if(radius<50 && radicont==1) {
radius=radius+1;
if(radius==49) {
radicont=0;
}
}
if(radius>10 && radicont==0) {
radius=radius-1;
if(radius==11) {
radicont=1;
}
}
}
}
else if (mouseButton == LEFT) {
resizeControl = 0;
}
}
}
}
I am aware of needing to use minim but I wasn't sure how to implement it. When I tried to add a song.play() to the if statement I would get a "token void" error or something along those lines. I know this means that it doesn't know what to play so I was wondering how to initiate the 'song' in the Ball class and get it to work with the collisions.
1