Objects moving at the same time

hi, i have made a random walker proggram and my goal is to be able to have a walker appear on the mouse position with every mouse click, but when a new one appears the previous one always stops moving, please help me. sorry if i’m making mistakes i’m new here.

Walker[] Walk = new Walker[200];


int total = 0;


void setup() {
  size(800, 600);
  background(0);
  for (int i = 0; i < Walk.length; i++) { 
    Walk[i] = new Walker();
  }
}





void mousePressed() {
}


void keyPressed() {
  total = total - 1;
}




void draw() {
  for (int i = 0; i < 1; i++) {
    Walk[i].step();
    Walk[i].render();
    Walk[i].startpo();
  }
}

//class

class Walker{
  float x;
  float y;
  
  int total = 0;
  
  Walker() {
  
  }
 
  
  
  void render() {
   stroke(102,240,240); 
   point(x,y);
   
    
  }
  
  
  
  void startpo() {
   if (mousePressed){
     x = mouseX;
     y = mouseY;
    total = total + 1;
   }
    
    
  }
  
  
  
  
  void step() {
   int choice = int(random(4));
    
    if (choice == 0) {
     x++; 
   } else if (choice == 1) {
     x--;
   } else if (choice == 2) {
     y++;
   } else {
     y--;
   }  
    
    
  }
  
  
  
  
  
  
  
  
  
}
1 Like

It happens because all your walkers are at the same position.

Every draw loop you run startpo() for every walkers. If you click your mouse, mousePressed will evaluate to true for all your walkers so you are setting all your wlakers to (mouseX, mouseY) and that’s why it seems that the old one stops.

You might instead want to consider an arrayList to handle the creation of your walkers, and use the mouseClicked() function to create your walker.

Here is a rewrite of your code using those ideas. As you can see, most of your code is the same:

ArrayList<Walker> walkers = new ArrayList<Walker>();

void setup() {
  size(800, 600);
  background(0);
}

void mouseClicked() {
  walkers.add(new Walker(mouseX, mouseY));
}

void draw() {
  for (int i = 0; i < walkers.size(); i++) {
    walkers.get(i).step();
    walkers.get(i).render();
  }
}

//class
class Walker {
  float x;
  float y;

  int total = 0;

  Walker( float x, float y) {
    this.x = x;
    this.y = y;
  }

  void render() {
    stroke(102, 240, 240); 
    point(x, y);
  }

  void step() {
    int choice = int(random(4));

    if (choice == 0) {
      x++;
    } else if (choice == 1) {
      x--;
    } else if (choice == 2) {
      y++;
    } else {
      y--;
    }
  }
}
2 Likes

It worked thank you so much