class Bubble {
color c;
// vertical velocity
float vy;
float x;
float y;
float d; // diameter
boolean newBall;
Bubble(float x, float y){
c = color(255);
vy = 0;
this.x = x;
this.y = y;
d = 1;
// this is a new ball!
newBall = true;
}
void display(){
// After the ball is created and is released...
/* I've placed this first in the condition as it's the state the ball will spend most time in.
The efficiency increase is probably negligible in this case... */
if(!newBall){
y = constrain(y, 0, height - d/2);
y = y + vy;
vy += gravity;
if (y >= height-d/2){
vy *= -0.9;
}
}
// Only true when ball first created and mouse is pressed
else {
if (d < 81){
d++;
}
// force release once it reaches maximum size...
else {
newBall = false;
}
}
// drawing the ball is independent of the above condition...
fill(c);
noStroke();
ellipseMode(CENTER);
ellipse(x, y, d, d);
}
}
Bubble [] bubbles = new Bubble[0];
/* I'm making the assumption that 'ac' is in fact gravity which should probably be a global property... */
float gravity = 0.1;
void setup(){
size (300,600);
smooth();
}
void draw(){
background(50);
//display balls
for (int i=0; i<bubbles.length; i++){
bubbles[i].display();
}
}
void mousePressed(){
//append new object directly to the array
bubbles = (Bubble[]) append(bubbles, new Bubble(mouseX, mouseY));
}
void mouseReleased(){
// in the array set the last ball's 'newBall' property to false:
// Note: bubbles.length is the number of balls in the array so the index of the last ball is bubbles.length-1
bubbles[bubbles.length-1].newBall = false;
}