Please help out with "Arrays" question. Cant seem to figure it out.
in
Programming Questions
•
1 year ago
Hey, ok so I have this code and I have to create an array of Ball objects. I need 25 objects instead of a single ball. the Ball has to be a class which is exactly what i've done. However, I cant create an array of 25 balls, the balls should vary a bit in sizes and larger balls have to fall down a little bit faster than smaller ones, I keep encountering errors and its not working, we have to use a loop im pretty sure. Here's the example I'm working off of --
http://www.openprocessing.org/sketch/22180 -- I've tried so many different ways but I cant seem to get it to work. So the way the code works is when you click the rectangle in the upper right corner it pauses the balls form falling, SO i pretty much need 25 balls to drop instead of 1 using an array. Please help me out, I'm going to paste my code below so you can take a look at it, Aprreciate any help :)
Ball oneBall;
boolean animate = true;
int btnX = 260;
int btnY = 50;
int btnW = 80;
int btnH = 80;
void setup() {
size(400, 500);
smooth();
oneBall = new Ball();
}
void draw () {
background(64);
oneBall.move();
oneBall.display();
if (animate == true) {
fill(200,100,20);
}
else { fill(100,20,20);
}
rect(btnX, btnY, btnW, btnH);
}
void mousePressed() {
if ( mouseX > btnX && mouseX < (btnX+btnW) && mouseY > btnY && mouseY < (btnY+btnH)) {
// toggle the value of on
// in other words, if on == true, then on becomes not true (false) and
// vice versa
animate = !animate; // on = (not)on
}
}
class Ball {
float speed;
float gravity;
float xpos;
float ypos ;
float dimensions;
Ball () {
speed = 0;
gravity = 0.1;
xpos = width/4;
ypos = -100;
dimensions = 50;
}
void display () {
ellipse (xpos, ypos, dimensions,dimensions);
}
void move() {
if (animate == true) {
ypos = ypos + speed;
speed = speed + gravity;
if (ypos >= height-120) {
// reduce the speed on every bounce to make it more
// realistic.. try -1 and see what happens
speed *= -0.9;
}
}
}
}
Ball oneBall;
boolean animate = true;
int btnX = 260;
int btnY = 50;
int btnW = 80;
int btnH = 80;
void setup() {
size(400, 500);
smooth();
oneBall = new Ball();
}
void draw () {
background(64);
oneBall.move();
oneBall.display();
if (animate == true) {
fill(200,100,20);
}
else { fill(100,20,20);
}
rect(btnX, btnY, btnW, btnH);
}
void mousePressed() {
if ( mouseX > btnX && mouseX < (btnX+btnW) && mouseY > btnY && mouseY < (btnY+btnH)) {
// toggle the value of on
// in other words, if on == true, then on becomes not true (false) and
// vice versa
animate = !animate; // on = (not)on
}
}
class Ball {
float speed;
float gravity;
float xpos;
float ypos ;
float dimensions;
Ball () {
speed = 0;
gravity = 0.1;
xpos = width/4;
ypos = -100;
dimensions = 50;
}
void display () {
ellipse (xpos, ypos, dimensions,dimensions);
}
void move() {
if (animate == true) {
ypos = ypos + speed;
speed = speed + gravity;
if (ypos >= height-120) {
// reduce the speed on every bounce to make it more
// realistic.. try -1 and see what happens
speed *= -0.9;
}
}
}
}
1