"Unreachable code" Error When Saving Frames

Hi,

When trying to record or saveFrame, I’m shown either “Unreachable code” or “missing active and static modes.” Does anyone know where and how to record the frames of this animation?

Thank you.

ArrayList<Circle> circles;
ArrayList<PVector> spots;
PImage img;

void setup() {
  size(411, 409);
  spots = new ArrayList<PVector>();
  img = loadImage("leobw.jpg");
  img.loadPixels();
  for (int x = 0; x < img.width; x++) {
    for (int y = 0; y < img.height; y++) {
      int index = x + y * img.width;
      color c = img.pixels[index];
      float b = brightness(c);
      if (b > 1) {
        spots.add(new PVector(x,y));
      }
      
    } 
  }
  circles = new ArrayList<Circle>();
}

void draw() {
  background(0);
  colorMode (HSB, 360, 100, 100);

  int total = 10;
  int count = 0;
  int attempts = 0;

  while (count <  total) {
    Circle newC = newCircle();
    if (newC != null) {
      circles.add(newC);
      count++;
    }
    attempts++;
    if (attempts > 1000) {
      noLoop();
      println("FINISHED");
      break;
    } 
  }


  for (Circle c : circles) {
    if (c.growing) {
      if (c.edges()) {
        c.growing = false;
      } else {
        for (Circle other : circles) {
          if (c != other) {
            float d = dist(c.x, c.y, other.x, other.y);
            if (d - 2 < c.r + other.r) {
              c.growing = false;
              break;
            }
          }
        }
      }
    }
    c.show();
    c.grow();
  }
}

Circle newCircle() {
  
  int r = int(random(0,spots.size()));
  PVector spot = spots.get(r);
  float x = spot.x;
  float y = spot.y;

  boolean valid = true;
  for (Circle c : circles) {
    float d = dist(x, y, c.x, c.y);
    if (d < c.r) {
      valid = false;
      break;
    }
  }

  if (valid) {
    return new Circle(x, y);
  } else {
    return null;
  }
}

class Circle {
  float x;
  float y;
  float r;

  boolean growing = true;

  Circle(float x_, float y_) {
    x = x_;
    y = y_;
    r = 1;
  }

  void grow() {
    if (growing) {
      r = r + 0.5;
    }
  }
  
  boolean edges() {
    return (x + r > width || x -  r < 0 || y + r > height || y -r < 0);
  }

  void show() {
    stroke(180, 360, 360);
    strokeWeight(0.2);
    noFill();
    ellipse(x, y, r/10, r/10);
  }
}
1 Like

Where do you call saveFrame? “Unreachable Code” errors have to do with code that will never be reached/executed because its “hidden” in an if statement that will never be true, for instance.

1 Like

Thank you so much Tony!

You were right. I was calling saveFrame in the completely wrong place.

It works just fine now.

Have a good one man!

2 Likes