Hey, i took a look at your code and there are some basic things missing, so maybe it would be a good idea to take a look at the OOP tutorial : http://processing.org/learning/objects/
but i will try to explain it with your code .
First thing to do is to create the array of objects. What you do is creating an array of ints. Thats not what we need. So instead of writing :
int[] Rectangles = new int[100];
you should actually write : Rectangles[] rects = new Rectangles[100];
do that at the beginning of your code. outside of the setup...
next step would be to initialize your objects.
thats similar to what do did here :
Rectangles = new Rectangles(xpos, ypos, 300, 250);
but, first you have to do that inside the setup and second you have to do it for all your 100 objects.
for(int i = 0; i < rects.length;i++){
rects[i] = new Rectangles(xpos, ypos);
}
I didnt know what, 300, 250 was for, that made no sense at this point so i removed it.
you still need a xpos, ypos, here. so i added a random function inside the loop :
float xpos = random(0,width);
float ypos = random(0,height);
if you use your variables, 300, 250 they all overlay and look like one.
Ok, if you try to start it now we still get an error.
Your class is missing its constructor. Its like the setup for your class.
where you pass the variable you created at the beginning.
Your class should look like this :
class Rectangles {
Rectangles(float x, float y){
} //thats what missing.
void move() {
}
void display() {
}
}
ok, now it still doenst work, because we need to draw our class. not only one but the 100 we created.
So inside your draw you need to create another for loop like this :
for(int i = 0; i < rects.length;i++){
rects[i].move();
rects[i].display();
}
So here is your code :
Quote:Rectangles[] rects = new Rectangles[100];
void setup() {
size(700, 700);
frameRate(24);
for(int i = 0; i < rects.length;i++){
float xpos = random(0,width);
float ypos = random(0,height);
rects[i] = new Rectangles(xpos, ypos);
}
}
void draw() {
background(255);
for(int i = 0; i < rects.length;i++){
rects[i].move();
rects[i].display();
}
}
class Rectangles {
float xpos = 0;
float ypos = 0;
Rectangles(float x, float y){
xpos = x;
ypos = y;
}
void move() {
xpos = xpos + 1;
if (xpos > width) {
xpos = 0;
}
ypos = ypos + 1;
if (ypos > height) {
ypos = 0;
}
}
void display() {
fill(0);
rect(xpos, ypos, 20, 10);
}
}