norris
YaBB Newbies
Offline
Posts: 2
Multiple instances of a class....maybe
Nov 23rd , 2009, 7:20pm
I am writing a sketch that displays a grid of circles that interact with the mouse cursor to change the size of the circles depending on the location of the mouse x and y. I have two classes: one for the circles and one for the force that manipulates the circles. Right now it does what I want it to do with a single force and then the up and down arrows control the range of the force. But I want to be able to use multiple forces at once. So I want to be able to click the mouse and lock the force in its current position and then get a new force that can be put anywhere in the field of circles and locked in its position with a mouse click and then get a new force and so on. I am new to Processing and I finding it difficult to understand how this would be structured exactly. I have tried numerous ways using mousePressed() (within void draw, within the Force class, as void mousePressed, etc.) but I am not sure if I am doing it correctly or if I am even writing the sketch in a way that will make it do what I want. So here is the sketch and any help would be greatly appreciated. You will see mousePressed the sketch but it is not doing anything at the moment. Circle [] cir = new Circle [900]; Force fr; void setup() { size(870 ,870); smooth(); for(int i = 0; i < cir.length; i++) { cir[i] = new Circle((i % 30) * 30, (i / 30) * 30, 30); fr = new Force(2,2); } } void draw() { background(220); for(int i = 0; i < cir.length; i++){ cir[i].display(); fr.force(); if (mousePressed) { fr.force(); } } } class Circle { float x, y, diameter; float d = 10; Circle(float xpos, float ypos, float dia) { x = xpos; y = ypos; diameter = dia; } void display() { fill(255); stroke(1); ellipse(x,y,diameter,diameter); diameter = (dist(fr.a, fr.b, x, y)/d); if (keyPressed) { if (keyCode == UP) { d+=1; } else if (keyCode == DOWN) { d-=1; } if (d < 1) { d = 1; } } } } class Force { float a, b; Force(float apos, float bpos) { a = apos; b = bpos; } void force() { fill (255,0,0); noStroke(); ellipse (a, b, 10, 10); a = mouseX; b = mouseY; } }