Problem with Coding Train 9.4

I’m trying to implement Daniel’s example of the bubbles array and I can’t for the life of me figure out why it’s not working. I should be getting 3 ascending bubbles, but I’m only getting 1, which I suspect might be all 3 on top of each other. Any help would be appreciated!

Bubble Class:

class Bubble {
  float x;
  float y;
  float diameter;

  Bubble(float tempD) {
    x = random(width);
    y = height;
    diameter = tempD;
  }

  void ascend() {
    y--;
  }
  void display() {
    stroke(0);
    fill(127);
    ellipse(x, y, diameter, diameter);
  }
}

Bubbles Sketch:

Bubble[] bubbles = new Bubble[2];

int i;

void setup() {
  size(640, 360);
  for (int i = 0; i < bubbles.length; i++) {
    bubbles[i] = new Bubble(30);
  }
}

void draw() {
  background(255);
  fill(0);
  for (int i = 0; i < bubbles.length; i++); 
  {
    bubbles[i].ascend();
    bubbles[i].display();
  }
}
1 Like

please format your code posting,
using the </> Prefromatted text button from the editor header menu

why not link to the book chapter / video / code
so we not have to search


please find the changes, different coding style …

//___________________________________________________________________Bubble Class:

class Bubble {
  float x,y,diam;
  Bubble(float _diam) {
    x = random(width);
    y = height;
    diam = _diam;
  }
  
  void ascend() {
    y--;
  }
  
  void display() {
    stroke(0);
    fill(127);
    ellipse(x, y, diam, diam);
    ascend();
  }
}

//_______________________________________________________________________Bubbles Sketch:

Bubble[] bubbles = new Bubble[2];

void setup() {
  size(640, 360);
  for (int i = 0; i < bubbles.length; i++ )  bubbles[i] = new Bubble(30);
}

void draw() {
  background(255);
  for (int i = 0; i < bubbles.length; i++)  bubbles[i].display();
}

your array only long 2, means index 0 , 1 only
why you not print

println(bubbles.length);
1 Like