Processing making object spawn after clicking object

Viewed 98

Hi this is my code the code should work like after clicking the new spawned cirlce another one should apear and after cliking the new one another one should spawn but it has some problem , for example i dont know what type of logic i should write for it? should i write recursion if yes then how?

float xpaikka =random(640);
float ypaikka =random(640) ;
int value_y = 0 ;
int value_x = 0 ;
void setup(){
  size(640,640);
  frameRate(500);
  background(34,45,323);
}


void draw(){
  ellipse(xpaikka,ypaikka,60,60);
  float randomi_x = random(630);
  float randomi_y = random(630);
  var d= dist(mouseX,mouseY,randomi_x,ypaikka);
  if (d < 30 && value_x != 0){
    ellipse(randomi_x,randomi_y,60,60);
    value_x = 0;
  }
}



void mousePressed() {
  if (value_x == 0) {
    value_x = mouseX;
    print("mouseX",value_x);
  } else {
    value_x = 0;
  }
}


void mousePressed2() {
  if (value_y == 0) {
    value_y = mouseY;
    print("mouseY",mouseY);
  } else {
    value_y = 0;
  }
}
1 Answers

Use an PVector to represent a the center point of the circle

PVector centerPoint = new PVector(random(640), random(640));

When the mouse button is pressed, calculate the distance from the mouse to the center of the circle. If the distance is less than the radius of the circle, create a new random center point:

PVector mousePoint = new PVector(mouseX, mouseY);
if (mousePoint.dist(centerPoint) <= 30.0) {
    centerPoint = new PVector(random(640), random(640));
}

Complete example:

PVector centerPoint = new PVector(random(640), random(640));

void setup() {
    size(640, 640);
}

void draw() {
    background(34, 45, 323);
    ellipse(centerPoint.x, centerPoint.y, 60, 60);
}

void mousePressed() {
    PVector mousePoint = new PVector(mouseX, mouseY);
    if (mousePoint.dist(centerPoint) <= 30.0) {
        centerPoint = new PVector(random(640), random(640));
    }
}
Related