How to move an object created on a loop

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);

}

}

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

1 Like

Hi @Ghost2,

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

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)

Works perfect. Thank You

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.

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);
  }
}

Please try it yourself.

Show your attempt and tell us where you are stuck

1 Like

I figured it out. Thanks for your help

1 Like

What was your solution? Can you share it please? -:slight_smile:

1 Like