Processing Forum
final float acceleration = 0.98; //acceleration due to gravity.
int numBalls = 0; // global var to keep track of how many balls are on the screen.
//initializing the balls to move around.
Ball ball1[100];
//Definition of what a "Ball" actually is.
class Ball {
float r;
float x,y;
float vx,vy;
float direction;
Ball(float p1, float p2, float p3, float p4, float p5, float p6){ // constructor
r = p1;
x = p2;
y = p3;
vx = p4;
vy = p5;
direction = p6;
}
}
void setup(){
size(400,400);
smooth();
}
void draw(){
background(255,255,255);
numBalls++;
if (numBalls >= ball1.length){
numBalls = 0;} //start array over
//Draw and move the balls
for (int i=0;i<numBalls;i++){
ball1[i].drawBall();
ball1[i].moveBall();
}
}
void mousePressed(){
ball1[numBalls] = new Ball (25, mouseX, 0, 0, 0, -1);
}
void drawBall(Ball aBall){
fill(33,232,23);
ellipse(aBall.x, aBall.y, aBall.r, aBall.r);
}
void moveBall(Ball aBall){
aBall.vy += acceleration;
aBall.y += aBall.vy;
if(aBall.y > height - 2.5){
aBall.vy *= aBall.direction;}
}