New to Processing help
in
Programming Questions
•
1 years ago
Hello,
I am new to processing and am looking for some help with arrays.
What I had done originally, is make a ball fall from the top of the screen due to gravity. The ball had to appear at a random x position due to gravity. This was for one ball.
I did this fine.
My next task was to change my code so that you have an array of 100 balls which when you press the mouse on the output window a ball falls from the x position where you click the mouse. The y position will always be the same, since it is falling from the top of the window. The array of 100 balls is so that every time you click the mouse, a ball falls.
So I changed my code to make an array of 100 balls. The array was just going to be called ball1 since that's what the one ball was called in my original code.
I am new to arrays and don't fully understand how to use them in code. So I am trying to learn. If someone could please help me with me code and how to get this to work and explain it if you can, that would be great.
Thanks in advance.
I am new to processing and am looking for some help with arrays.
What I had done originally, is make a ball fall from the top of the screen due to gravity. The ball had to appear at a random x position due to gravity. This was for one ball.
I did this fine.
My next task was to change my code so that you have an array of 100 balls which when you press the mouse on the output window a ball falls from the x position where you click the mouse. The y position will always be the same, since it is falling from the top of the window. The array of 100 balls is so that every time you click the mouse, a ball falls.
So I changed my code to make an array of 100 balls. The array was just going to be called ball1 since that's what the one ball was called in my original code.
I am new to arrays and don't fully understand how to use them in code. So I am trying to learn. If someone could please help me with me code and how to get this to work and explain it if you can, that would be great.
Thanks in advance.
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;}
}
1