Several elements interactivity

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!

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
  
 }

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

there is options for mouse like mouseOver and
mousePressed try them

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.

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
)



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

1 Like

Thank you so much for your help!

1 Like