We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Using arrays and classes
Page Index Toggle Pages: 1
Using arrays and classes (Read 564 times)
Using arrays and classes
Sep 27th, 2009, 6:14pm
 
Hey, I'm working on a program that will have 100 moving rectangles.  So far I have this for code:

Rectangles = new Rectangles(xpos, ypos, 300, 250);

void setup() {
 
size(700, 700);
frameRate(24);
 
}

void draw() {
 
 background(255);
 move();
 display();
 
}



int[] Rectangles = new int[100];

class Rectangles {

float xpos = 0;
float ypos = 0;
 
void move() {
 
 xpos = xpos + 1;
 if (x > width) {
    x = 0;
 }
 
 ypos = ypos + 1;
 if (ypos > height) {
  y = 0;
 }
}

void display() {
fill(0);
rect(xpos, ypos, 20, 10);
 
}
 
 
}


Right now I'm getting an error that says "unexpected error: token" which I don't understand.

Furthermore, for the array that I'm making for the rectangles I'm not sure what to do other than declare it.  I don't know how to actually use the array in the program so that I actually get 100 rectangles, so how do you use the elements of an array after you declare it?
Re: Using arrays and classes
Reply #1 - Sep 27th, 2009, 10:19pm
 
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);

  }


}



Page Index Toggle Pages: 1