Why doesn't this work?
in
Programming Questions
•
2 years ago
I was doing some coding then all of a sudden without changing anything (that i am aware of) it just stopped working.
Can you guys please tell me what you see is wrong with it?
Main code:
int maxBalls = 20;
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;
//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-1; i++) {
ballArray[i].display();
ballArray[i].update();
ballArray[i].mouse();
}
}
void mousePressed() {
if (mouseButton == LEFT) {
if (ballCount < maxBalls) {
ballArray[ballCount] = new Ball(mouseX, mouseY, random(-2, 2), random(-2, 2), 10.0);
ballCount++;
if(ballCount == maxBalls) {
ballCount = maxBalls - ballCount;
}
}
}
Ball class code:
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 the error and what it means but whatever i change it just wont work.
thanks for the help.
Can you guys please tell me what you see is wrong with it?
Main code:
int maxBalls = 20;
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;
//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-1; i++) {
ballArray[i].display();
ballArray[i].update();
ballArray[i].mouse();
}
}
void mousePressed() {
if (mouseButton == LEFT) {
if (ballCount < maxBalls) {
ballArray[ballCount] = new Ball(mouseX, mouseY, random(-2, 2), random(-2, 2), 10.0);
ballCount++;
if(ballCount == maxBalls) {
ballCount = maxBalls - ballCount;
}
}
}
Ball class code:
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 the error and what it means but whatever i change it just wont work.
thanks for the help.
1