# Several elements interactivity

**URL:** https://discourse.processing.org/t/several-elements-interactivity/31081
**Category:** Coding Questions
**Created:** [July 6, 2021, 12:51am UTC](https://discourse.processing.org/t/several-elements-interactivity/31081 "2021-07-06T00:51:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![humano](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@humano](https://discourse.processing.org/u/humano)
#### Post date: [July 6, 2021, 12:51am UTC](https://discourse.processing.org/t/several-elements-interactivity/31081/1 "2021-07-06T00:51:45Z")

</div>

Hi there! I have learned how to move an element with mouseX and mouseY, in this case, a red dot. Now I want to stop red dote and move another blue dot with my finger. ¿How could I do it? Thanks!

```auto
void setup() {
  size(1000, 1000);
  noStroke ();
  background(255,255,255);
  }
  
void draw() {
  fill(255,0,0);
  ellipse(mouseX,mouseY,100,100);
  fill(0,0,255);
  ellipse(500,500,100,100); //the dot that I want to move too
  
 }

```

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [July 6, 2021, 2:52am UTC](https://discourse.processing.org/t/several-elements-interactivity/31081/2 "2021-07-06T02:52:25Z")

</div>

how with your finger ?? are you using touch screen ???

there is options for mouse like mouseOver and  
`mousePressed` try them

---

<div class="post-metadata">

### Author: ![humano](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@humano](https://discourse.processing.org/u/humano)
#### Post date: [July 6, 2021, 8:56am UTC](https://discourse.processing.org/t/several-elements-interactivity/31081/3 "2021-07-06T08:56:24Z")

</div>

Yes, what I want is to move every dote when I touch it and leave it motionless when I don’t touch it. Thanks, I will try with these the things.

---

<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 6, 2021, 8:59am UTC](https://discourse.processing.org/t/several-elements-interactivity/31081/4 "2021-07-06T08:59:54Z")

</div>

> [@humano](#):
>
> `ellipse(mouseX,mouseY,100,100);`

That won’t work when you have more than one ball.

Instead use variables: ellipse (x5,y5,100,100);

Now you can use dist() to find out on which ball the mouse has been clicked. That one is the active ball. Now you can say x5=mouseX; y5=mouseY;

So whenever mousePressed () is called check dist to all balls; when near, set activeBallNumber to 5; or whatever. Make it a global variable.

Yeah and in draw() when activeBallNumber is \> 0 use if(activeBallNumber==5) { etc. to distinguish which ball to move.

In mousereleased say activeBallNumber=-1;

Before setup: int activeBallNumber=-1;

(  
In fact that is hard when you don’t use an array

So store the balls in an array and loop over it. Makes your life easier  
)

```auto

int activeBallNumber=-1;

float x1=100, y1=200, 
  x5=200, y5=300;

void setup() {
  size(1000, 1000);
  noStroke ();
  background(255, 255, 255);
}

void draw() {
  background(255); 

  fill(255, 0, 0);
  ellipse (x5, y5, 100, 100);

  fill(0, 0, 255);
  ellipse(x1, y1, 100, 100); // 

  if ( activeBallNumber >=0) {
    if (activeBallNumber ==1) {
      x1=mouseX; 
      y1=mouseY;
    } else if (activeBallNumber ==5) {
      x5=mouseX; 
      y5=mouseY;
    }
  }
}

//----------------------------------------------------------------

void mousePressed() {
  if (dist(mouseX, mouseY, x1, y1)< 110) {
    activeBallNumber= 1;
  } else if (dist(mouseX, mouseY, x5, y5)< 110) {
    activeBallNumber=5;
  }
}

void mouseReleased() {
  activeBallNumber=-1;
}
//

```

---

<div class="post-metadata">

### Author: ![humano](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@humano](https://discourse.processing.org/u/humano)
#### Post date: [July 6, 2021, 9:44pm UTC](https://discourse.processing.org/t/several-elements-interactivity/31081/5 "2021-07-06T21:44:17Z")

</div>

Thank you so much for your help!
