# How to move an object created on a loop

**URL:** https://discourse.processing.org/t/how-to-move-an-object-created-on-a-loop/38034
**Category:** Beginners
**Created:** [July 19, 2022, 8:16pm UTC](https://discourse.processing.org/t/how-to-move-an-object-created-on-a-loop/38034 "2022-07-19T20:16:34Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Ghost2](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@Ghost2](https://discourse.processing.org/u/Ghost2)
#### Post date: [July 19, 2022, 8:16pm UTC](https://discourse.processing.org/t/how-to-move-an-object-created-on-a-loop/38034/1 "2022-07-19T20:16:34Z")

</div>

I have this piece of code where i am trying to move an object i.e three dots created by a for loop and i want to move the dots when mouse is pressed eg. Move the dots like dot++.If anyone can help me it would be great\

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\  
int dotX=0;  
int dotY=0;  
int Radius=25;

void setup(){  
size(700,700);  
}  
void draw(){  
dotX=width/2;  
dotY=height/2;  
background(200);  
for(int i=0;i\<3;i++){  
circle(dotX,dotY+Radius\*i,Radius);

}

}

---

<div class="post-metadata">

### Author: ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)
#### Post date: [July 19, 2022, 8:31pm UTC](https://discourse.processing.org/t/how-to-move-an-object-created-on-a-loop/38034/2 "2022-07-19T20:31:15Z")

</div>

You can use the mousePressed() function.  
It goes outside of the draw function.

> **[Reference](https://processing.org/reference/mousePressed_.html)**
>
> The mousePressed() function is called once after every time a mouse button is pressed. The mouseButton variable (see the related reference entry) can be used to determine which button ha…

---

<div class="post-metadata">

### Author: ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)
#### Post date: [July 20, 2022, 5:52am UTC](https://discourse.processing.org/t/how-to-move-an-object-created-on-a-loop/38034/3 "2022-07-20T05:52:21Z")

</div>

Hi @Ghost2,

not exactly clear to me what you wanna do, but you can do for example s.th like

```auto
int dotX=0;
int dotY=0;
int Radius=25;

void setup() {
  size(700, 700);
  dotX=Radius/2;
  dotY=height/2;
}
void draw() {  
  background(200);
  if (mousePressed) {
    dotX = (dotX+5) % (width-(Radius/2));
  }
  for (int i=0; i<3; i++) {
    circle(dotX, dotY+Radius*i, Radius);
  }
}

```

if you want to move every dot individually you need another approach.

Cheers  
— mnse

PS: If you wonder why Radius/2 read the [circle reference (default settings)](https://processing.org/reference/circle_.html)

---

<div class="post-metadata">

### Author: ![Ghost2](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@Ghost2](https://discourse.processing.org/u/Ghost2)
#### Post date: [July 20, 2022, 7:49pm UTC](https://discourse.processing.org/t/how-to-move-an-object-created-on-a-loop/38034/4 "2022-07-20T19:49:44Z")

</div>

Works perfect. Thank You

---

<div class="post-metadata">

### Author: ![Ghost2](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@Ghost2](https://discourse.processing.org/u/Ghost2)
#### Post date: [July 20, 2022, 8:10pm UTC](https://discourse.processing.org/t/how-to-move-an-object-created-on-a-loop/38034/5 "2022-07-20T20:10:06Z")

</div>

And now to the code if i press the mouse and want to shift the circles to line up in the x axis///////If mousePresssed the circles should face the width and start moving towards the width in a straight line.

```auto
int dotX=0;
int dotY=0;
int Radius=25;

void setup() {
  size(700, 700);
  dotX=Radius/2;
  dotY=height/2;
}
void draw() {  
  background(200);
 // if (mousePressed) {
  // dotX = (dotX+5) % (width-(Radius/2));
 // }
  for (int i=0; i<3; i++) {
    circle(dotX, dotY+Radius*i, Radius);
  }
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 20, 2022, 8:31pm UTC](https://discourse.processing.org/t/how-to-move-an-object-created-on-a-loop/38034/6 "2022-07-20T20:31:29Z")

</div>

Please try it yourself.

Show your attempt and tell us where you are stuck

---

<div class="post-metadata">

### Author: ![Ghost2](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@Ghost2](https://discourse.processing.org/u/Ghost2)
#### Post date: [July 21, 2022, 3:06am UTC](https://discourse.processing.org/t/how-to-move-an-object-created-on-a-loop/38034/7 "2022-07-21T03:06:09Z")

</div>

I figured it out. Thanks for your help

---

<div class="post-metadata">

### Author: ![JSGauthier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jsgauthier/32/9910_2.png) [@JSGauthier](https://discourse.processing.org/u/JSGauthier)
#### Post date: [July 25, 2022, 4:40pm UTC](https://discourse.processing.org/t/how-to-move-an-object-created-on-a-loop/38034/8 "2022-07-25T16:40:49Z")

</div>

What was your solution? Can you share it please? -🙂
