MitraM
YaBB Newbies
Offline
Posts: 5
array and classes
Mar 17th , 2010, 4:19pm
hi, i'm using processing since two weeks and have problems with putting a class into an array. it's not working. i think the structure is ok, but somewhere is a mistake.... i'm sitting since a week now and going from one error to the next.... this is the code: Boid[] boidies = new Boid[150]; // amount of boids int canvasX = 700; // canvas size x-direction int canvasY = 500; // canvas size y-direction int r = 0; // color rgb: r-value int g = 255; // color rgb: g-value int b = 255; // color rgb: b-value float cDist = 50; // length of connecting lines float inc = 0.001; // move increasement every loop float[] ptsX; float[] ptsY; float[] posX; float[] posY; void setup() { size(canvasX, canvasY); smooth(); stroke(0, 170, 250); strokeWeight(1); background(255); ellipseMode(CENTER); for (int i = 0; i < boidies.length; i++) { ptsX[i] = random(0, canvasX); ptsY[i] = random(0, canvasY); posX[i] = ptsX[i]; posY[i] = ptsY[i]; } for (int i = 0; i < boidies.length; i++) { boidies[0] = new Boid(posX[i],posY[i]); } } void draw() { background(0); fill(255); stroke(r, g, b, 200); strokeWeight(1); for (int i = 0; i < boidies.length; i++) { boidies[i].display(); boidies[i].connect(); boidies[i].move(); } } class Boid { float[] ptsX; float[] ptsY; float[] posX; float[] posY; Boid (posX, posY) { ptsX[i] = random(0, canvasX); ptsY[i] = random(0, canvasY); posX[i] = ptsX[i]; posY[i] = ptsY[i]; posX = posX[i]; posY = posY[i]; } void move() { // Update the boids' positions for (int i = 0; i < boidies.length; i++) { posX[i] = noise(ptsX[i]) * canvasX; posY[i] = noise(ptsY[i]) * canvasY; ptsX[i] = ptsX[i] + inc; ptsY[i] = ptsY[i] + inc; } } void connect() { // draw the connecting lines for (int i = 0; i < boidies.length-1; i++) { for (int j = i; j < boidies.length; j++) { if (dist(posX[j], posY[j], posX[i], posY[i]) < cDist) { line(posX[j], posY[j], posX[i], posY[i]); } } } } void display() { // draw the boids (draw them last, so they are not masked by lines) for (int i = 0; i < boidies.length; i++) { ellipse(posX[i], posY[i], 4, 4); } } } maybe someone has an idea??? thank you!!!!