Changing size of my bubbles separately

Hello, I am using classes to make bubbles come up the screen and I was wondering why when I pop the bubbles or if the bubbles go out of the screen they all change at the same time. I want them to respawn with their own separate size.

bubble b1;
bubble b2;
bubble b3;
bubble b4;
float size = random(20,40);

void setup(){
  size(500,600);
  b1 = new bubble(random(0,width-15),height,10,random(2,5),color(random(0,255),0,100));
  b2 = new bubble(random(0,width-15),height,10,random(2,5),color(random(0,255),0,100));
  b3 = new bubble(random(0,width-15),height,10,random(2,5),color(random(0,255),0,100));
  b4 = new bubble(random(0,width-15),height,10,random(2,5),color(random(0,255),0,100));
}


void draw(){
  background(255);
  b1.display();
  b2.display();
  b3.display();
  b4.display();
  b1.rise();
  b2.rise();
  b3.rise();
  b4.rise();
  b1.jiggle();
  b2.jiggle();
  b3.jiggle();
  b4.jiggle();
  b1.Pop();
  b2.Pop();
  b3.Pop();
  b4.Pop();
  b1.miss();
  b2.miss();
  b3.miss();
  b4.miss();
}

class bubble{
  float x;
  float y;
  float dx;
  float dy;
  color rcolor;
  
  
  bubble(float x, float y, float dx, float dy, color rcolor ){
    this.x=x;
    this.y=y;
    this.dx=dx;
    this.dy=dy;
    this.rcolor=rcolor;
  }
void display() {
  fill(rcolor);
  circle(x,y,size);
}

void rise(){
  y = y- dy;
}

void jiggle(){
  x = x + random(-2,2);
}
void Pop(){
  if(mousePressed && mouseX>=x-size/2 && mouseX<=x+size/2 && mouseY>=y-size/2 && mouseY<=y+size/2){
    y = height+50;
    size = random(20,40);
    x = random(width-15);
    rcolor = color(random(0,255),0,120);
    dy+=0.5;
  }
}

void miss(){
  if(y<0){
    size = random(20,30);
    y = height;
    x = random(width-15);
    rcolor = color(random(0,255),0,120);
    dy-=0.5;
}
}
  
}

Hi @FloorBean

you were setting the size as a global variable. So when you are poping one, the size affects all bubbles
I modified your class as:

class bubble {
  float x;
  float y;
  float dx;
  float dy;
  float sizeBubble;
  color rcolor;


  bubble(float x, float y, float dx, float dy, color rcolor ) {
    this.x=x;
    this.y=y;
    this.dx=dx;
    this.dy=dy;
    this.rcolor=rcolor;
    this.sizeBubble = random(20,40);
  }

  void display() {
    fill(rcolor);
    circle(x, y, sizeBubble);
  }

  void rise() {
    y = y- dy;
  }

  void jiggle() {
    x = x + random(-2, 2);
  }

  void Pop() {
    if (mousePressed && mouseX>=x-sizeBubble/2 && mouseX<=x+sizeBubble/2 && mouseY>=y-sizeBubble/2 && mouseY<=y+sizeBubble/2) {
      y = height+50;
      sizeBubble = random(20, 40);
      x = random(width-15);
      rcolor = color(random(0, 255), 0, 120);
      dy+=0.5;
    }
  }

  void miss() {
    if (y<0) {
      sizeBubble = random(20, 30);
      y = height;
      x = random(width-15);
      rcolor = color(random(0, 255), 0, 120);
      dy-=0.5;
    }
  }
}

So, now you have sizeBubble to define the size of each bubble and it will only affect that one.

4 Likes