# Objects moving at the same time

**URL:** https://discourse.processing.org/t/objects-moving-at-the-same-time/22640
**Category:** Coding Questions
**Created:** [July 15, 2020, 5:54pm UTC](https://discourse.processing.org/t/objects-moving-at-the-same-time/22640 "2020-07-15T17:54:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![luimi](https://avatars.discourse-cdn.com/v4/letter/l/96bed5/32.png) [@luimi](https://discourse.processing.org/u/luimi)
#### Post date: [July 15, 2020, 5:54pm UTC](https://discourse.processing.org/t/objects-moving-at-the-same-time/22640/1 "2020-07-15T17:54:10Z")

</div>

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.

```auto
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--;
   }  
    
    
  }
  
  
  
  
  
  
  
  
  
}

```

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [July 15, 2020, 6:35pm UTC](https://discourse.processing.org/t/objects-moving-at-the-same-time/22640/2 "2020-07-15T18:35:53Z")

</div>

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:

```auto
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--;
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![luimi](https://avatars.discourse-cdn.com/v4/letter/l/96bed5/32.png) [@luimi](https://discourse.processing.org/u/luimi)
#### Post date: [July 16, 2020, 4:08pm UTC](https://discourse.processing.org/t/objects-moving-at-the-same-time/22640/3 "2020-07-16T16:08:33Z")

</div>

It worked thank you so much
