applying actions to individual objects
Fired up Processing for the first time in a while, for some quick prototyping, and my brain has gone awry. Could somebody point me in the right direction for the following:
So, I have a load of 'Sphere' objects, randomly bunged on the stage. I then want to attach user actions to individual elements ie. click on a sphere and it does something like increase in size (for now).
Here's my code:
//Set up object arrays
int numOfspheresArr = 20;
Sphere[] spheresArr = new Sphere[numOfspheresArr];
void setup(){
size(800, 600);
for (int i = 0; i < spheresArr.length; i++){
spheresArr[i] = new Sphere (color(random(0,255),0,0,60),50,random(0, width), random(0, height));
}
}
void draw(){
background(255);
for(int i = 0; i < spheresArr.length; i++){
spheresArr[i].display();
spheresArr[i].hitTest();
}
}
// Class : Begin
class Sphere{
color c;
float diameter;
float xpos;
float ypos;
Sphere(color _c, float _diameter, float _xpos, float _ypos){
c = _c;
diameter = _diameter;
xpos = _xpos;
ypos = _ypos;
}
void display(){
noStroke();
smooth();
fill(c);
ellipseMode(CENTER);
ellipse(xpos, ypos, diameter, diameter);
}
void hitTest(){
if (mousePressed == true){
this.diameter ++;
println(this + " object is being clicked");
}
}
}
// Class : End
What am I missing here again? Soz for the gormless post!
