Increase objects in sketch

Hello,

I am trying to increase the objects in a sketch through duplication by increasing the values in an array, but when I try to use expand(), the sketch doesn't run (NullPointerException). When I take expand() out, it runs fine.

Here is the code:

Pulse[] beat = new Pulse[1];

void setup() {
  size(500,500,P3D);

  //bubbles
  for (int i=0; i < beat.length; i++) {
    beat[i] = new Pulse (random(width), random(height), 50, random(50));
  }
}

void draw() {    
  background(255);
  noCursor();

  for (int i=0; i < beat.length; i++) {
    beat[i].multiply();
    beat[i].quiver();
    beat[i].display();
  } 
}

class Pulse {
  float x;
  float y;
  float r;
  float alp;
  float dx = 5;
  float dy = 5;
  int heart = 0;

  //Constructor
  Pulse(float tempX, float tempY, float tempR, float tempAlp) {
    x = tempX;   
    y = tempY;
    r = tempR;
    alp = tempAlp;
  }

  void display() {

    color c = color(138, 3, 3, alp);
    stroke(0, 25);
    fill(c);
    ellipse(x, y, r*2, r*2);
  }

  void quiver() {
    float tx, ty;

    //Keeps x coordinate within boundaries
    tx = x+round(random(-dx, dx));
    while (tx<0 || tx>width)
      tx=x+round(random(-dx, dx));

    //Keeps y coordinate within boundaries
    ty=y+round(random(-dy, dy));
    while (ty<0 || ty>height)
      ty=y+round(random(-dy, dy));

    x = tx;
    y = ty;
  }
  void multiply() {
      beat = (Pulse[]) expand(beat);
  }
}
Tagged:

Answers

Sign In or Register to comment.