ball color.
in
Programming Questions
•
1 years ago
how come only one ball changes color?
I have put in an int2color () object, but it doesnt work in draw(). What am i missing please?
Ball ball1;
Ball ball2;
Ball ball2;
void setup(){
size (500,500);
ball1 = new Ball(32);
ball2 = new Ball (16);
}
size (500,500);
ball1 = new Ball(32);
ball2 = new Ball (16);
}
void draw(){
background(0);
ball1.display();
ball1.move();
ball2.display();
ball2.move();
if (ball1.intersect(ball2)){
ball2.int2color();
ball1.intcolor();
}
}
class Ball{
float rad;
float x,y;
float xspeed,yspeed;
Ball(float _rad){
rad= _rad;
x = random(width);
y = random(height);
xspeed = random (-5, 2);
yspeed = random (-5,2);
}
void display(){
ellipse (x,y,rad,rad);
fill (255);
if (x>width||x<0){
xspeed *= -1;}
if (y>height||y<0){
yspeed *= -1;}
}
void intcolor(){
fill (255,0,0);
}
void int2color(){
fill (0,255,0);
}
void move(){
x += xspeed;
y += yspeed;
}
boolean intersect (Ball other){
float distance = dist (x, y, other.x, other.y);
if (distance < rad + other.rad){
return true;
}else{
return false;
}
}
}
background(0);
ball1.display();
ball1.move();
ball2.display();
ball2.move();
if (ball1.intersect(ball2)){
ball2.int2color();
ball1.intcolor();
}
}
class Ball{
float rad;
float x,y;
float xspeed,yspeed;
Ball(float _rad){
rad= _rad;
x = random(width);
y = random(height);
xspeed = random (-5, 2);
yspeed = random (-5,2);
}
void display(){
ellipse (x,y,rad,rad);
fill (255);
if (x>width||x<0){
xspeed *= -1;}
if (y>height||y<0){
yspeed *= -1;}
}
void intcolor(){
fill (255,0,0);
}
void int2color(){
fill (0,255,0);
}
void move(){
x += xspeed;
y += yspeed;
}
boolean intersect (Ball other){
float distance = dist (x, y, other.x, other.y);
if (distance < rad + other.rad){
return true;
}else{
return false;
}
}
}
1