Objects each adding new object to arraylist
in
Programming Questions
•
7 months ago
Hi all, I'm very new to programming. Ive got a simple sketch here that uses an array of "Robots" and those robots do "Poo"s on the canvas at their location. The problem is I am adding new Poos to the location of only 1 Robot. I can't think of the solution... any help would be appreciated... im pretty sure its a simple fix, I think the issue is in the Poo class.
Cheers!
- Robot[] army = new Robot[4];
- ArrayList <Poo> poops = new ArrayList();
- int time;
- int wait = 1000;
- void setup () {
- size(400, 400);
- time = millis();
- for (int i = 0; i < army.length; i++)
- {
- army[i] = new Robot();
- }
- poops.add(new Poo(0,0));
- }
- void draw () {
- background(255);
- for (int i = 0; i < army.length; i++) {
- army[i].display();
- }
- if (millis() - time >= wait) {
- println(poops.size());
- for (int i = 0; i < army.length; i++) {
- army[i].move();
- army[i].pooper();
- }
- time = millis();
- }
- stroke (140);
- for (int i = 1; i < poops.size(); i++) {
- poops.get(i).display();
- line(poops.get(i).x, poops.get(i).y, poops.get(i-1).x, poops.get(i-1).y);
- }
- }
- class Robot {
- float x = (width/2);
- float y = (height/2);
- float r = 14;
- Robot () {
- }
- void move () {
- x = x + random(-50, 50);
- y = y + random(-50, 50);
- if (x > width) { x = width;}
- if (x < 0) { x = 0;}
- if (y > height) { y = height;}
- if (y < 0) {y = 0;}
- }
- void display () {
- fill(150);
- ellipse (x, y, r, r);
- }
- void pooper () {
- poops.add(new Poo(x,y));
- }
- }
- class Poo {
- float x;
- float y;
- Poo (float x_ , float y_ ) {
- for (int i = 0; i < army.length; i++) {
- x = army[i].x;
- y = army[i].y;
- }
- }
- void display() {
- fill(0);
- ellipse(x, y, 5, 5);
- }
- }
1