# How to use mouseClicked to only effect one object in ArrayList?

**URL:** <https://discourse.processing.org/t/how-to-use-mouseclicked-to-only-effect-one-object-in-arraylist/22067>\
**Category:** Coding Questions\
**Created:** [June 22, 2020, 3:48pm UTC](https://discourse.processing.org/t/how-to-use-mouseclicked-to-only-effect-one-object-in-arraylist/22067 "2020-06-22T15:48:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![s.k9](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@s.k9](https://discourse.processing.org/u/s.k9)\
**Post date:** [June 22, 2020, 3:48pm UTC](https://discourse.processing.org/t/how-to-use-mouseclicked-to-only-effect-one-object-in-arraylist/22067/1 "2020-06-22T15:48:22Z")

</div>

Hi all,

I am trying to make a simple game involving legos. The premise is the user presses ‘d’ to add legos and can click on them to move them around. On hovering over a lego, it changes color, and on click, the lego follows the mouse. Upon a second click, the lego is again placed. Right now, when I add only one lego, the code works fine. However, when I add more than one lego and I click on one, I grab all the legos… not just the one I clicked on. How would I only grab the one I am hovering over? I would really appreciate any help you can provide me with. Thanks!

```auto
boolean isDragging = false;
...
public void mouseClicked() {
...
 if (mode == 1) { //mode 1 is toyInteraction
    isDragging = !isDragging;
  }
}
...
private void toyInteraction() { 
  //call hoverLego, for each loop
  for (Lego lg : Legos) {
    if (lg.hoverLego()) {
      fill(255); //lego color, stroke color and weight, all when legoHover is true
      stroke(0); 
      strokeWeight(5);
    }
    if (isDragging == true) {
      //println("is dragging");
      lg.x = mouseX-lg.w/2;
      lg.y = mouseY-lg.h/2;
    }
  }
}
...
boolean hoverLego() {
    if ((x+w > mouseX && mouseX > x) && (y < mouseY && mouseY < y+h)) {
      p.disableStyle();
      return true;
    } else {
      p.enableStyle();
      return false;
    }
    //return false; //regardless of for loop, still false
  }
...

```

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 22, 2020, 4:06pm UTC](https://discourse.processing.org/t/how-to-use-mouseclicked-to-only-effect-one-object-in-arraylist/22067/2 "2020-06-22T16:06:54Z")

</div>

Your Lego class should have a boolean field that sets the state as “currently-being-dragged” so each Lego instance has this field and tehnically only one lego would have this state set to true. I did a demo [here](https://discourse.processing.org/t/change-color-moving/21924/4) recently. Maybe you can sere the approach there and applied it to your use case.

Btw, the code you provide is not enough to see the problem. I suggest you provide a [MCVE](https://stackoverflow.com/help/minimal-reproducible-example) as it will help a ton in these cases.

Kf

---

<div class="post-metadata">

**Author:** ![s.k9](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@s.k9](https://discourse.processing.org/u/s.k9)\
**Post date:** [June 22, 2020, 4:13pm UTC](https://discourse.processing.org/t/how-to-use-mouseclicked-to-only-effect-one-object-in-arraylist/22067/3 "2020-06-22T16:13:20Z")

</div>

Hi kfrajer, thanks for your insight, I really appreciate it!
