errors on creating new object in the same class

edited September 2017 in Questions about Code

I have encountered a problem, when the bacteria replicate there is and bug that causes the program to crash please and thank you!

ArrayList <antibody> antibodies = new ArrayList<antibody>();
ArrayList <virus> viruses = new ArrayList<virus>();
ArrayList <macrophage> macrophages = new ArrayList<macrophage>();
ArrayList <killer> killers = new ArrayList<killer>();
ArrayList <bacteria> bacterias = new ArrayList<bacteria>();
void setup() {
  surface.setResizable(true);
  bacteria b = new bacteria();
  bacterias.add(b);
  size(1000, 700);
  strokeWeight(8);
  stroke(255);
}
void draw() {
  background(150, 50, 50);
  text(viruses.size(),100,100);
  if (keyPressed) {
    macrophage c = new macrophage(random(width), random(height), 1);
    macrophages.add(c);
  }
  if (mousePressed) {
    virus v = new virus();
    viruses.add(v);
  }
  for (int v = 0; v<viruses.size(); v++) {
    virus virus = viruses.get(v);
    virus.exist();
    if (!virus.isalive) {
      viruses.remove(v);
    }
  }
  for (int b = 0; b<bacterias.size(); b++) {
    bacteria bacterium = bacterias.get(b);
    bacterium.exist(bacterium);
    //        if (frameCount%60==0&&bacterium.isalive&&bacterias.size()<10) {
    //  bacteria rep = new bacteria();
    //  bacterias.add(rep);
    //}
    if (!bacterium.isalive) {
      viruses.remove(b);
    }
  }
  for (int macrophage = 0; macrophage<macrophages.size(); macrophage ++) {
    macrophage c = macrophages.get(macrophage);
    if (!c.isalive) {
      macrophages.remove(c);
    }
    for (int v = 0; v<viruses.size(); v++) {
      virus virus = viruses.get(v);
      c.fight(virus);
    }
    c.display();
  }
}
//====================================================
class macrophage {
  float x, y, type, size, untilldeath;
  boolean busy = false, moved = false, isalive = true, infected = false;
  PVector randir = new PVector(random(-4, 4), random(-4, 4));
  macrophage(float x1, float y1, float type1) {
    x = x1;
    y = y1;
    type = type1;
    size = random(40, 42);
  }
  void display() {
    if (x>width|| x<0) {
      randir.x*=-1;
    }
    if (y>height|| y<0) {
      randir.y *= -1;
    }
    if (!busy) {
      x += randir.x;
      y += randir.y;
      randir.x += random(-0.1, 0.1);
      randir.y += random(-0.1, 0.1);
    }
    if (infected) {
      fill(100, 50, 100);
      ellipse(x, y, size, size);
      if (frameCount%5==0) {
        virus v = new virus(x, y);
        viruses.add(v);
        v.incell = true;
      }
      untilldeath+=1;
    } else {    
      fill(255, 100, 50);
      ellipse(x, y, size, size);
    }
    moved = false;
  }
  void fight(virus virus) {
    if (untilldeath > 500) {
      isalive = false;
      if (virus.x == x && virus.y == y) {
        virus.incell = false;
      }
    }
    if (!moved) {
      if (type == 1) {//macrophage
        if (dist(virus.x, virus.y, x, y)<size/2 && random(25)<10&&!infected&& !virus.incell) {
          infected = true;
          busy = true;
          virus.isalive = false;
        } else if (dist(virus.x, virus.y, x, y)<size/2&&!infected&&!virus.incell) {
          virus.isalive = false;
        }
        if ((dist(virus.x, virus.y, x, y)<size*2)&& random(50)<5 && !virus.incell&& !infected) {
          line(virus.x, virus.y, x, y);
          virus.isalive = false;
        }
      }
    }
  }
}
//============================================
class killer {
}
//===========================================
class virus {
  float x, y, randirx, randiry;
  boolean isalive = true, incell ;
  virus(float x1, float y2) {
    x = x1;
    y = y2;
    randirx = random(-1, 1);
    randiry = random(-1, 1);
  }
  virus() {
    x = random(width);
    y = random(height);
    randirx = random(-1, 1);
    randiry = random(-1, 1);
  }
  void exist() {
    if (x>width|| x<0) {
      randirx*=-1;
    }
    if (y>height|| y<0) {
      randiry *= -1;
    }
    if (!incell) {
      strokeWeight(1);
      stroke(0);
      x += randirx;
      y += randiry;
      randirx += random(-0.01, 0.01);
      randiry += random(-0.01, 0.01);
      fill(250, 100, 250);
      rect(x, y, 7, 7);
      strokeWeight(8);
      stroke(255);
    }
  }
}
//==========================================
class bacteria {
  float x, y, randirx, randiry;
  boolean isalive = true;
  bacteria(float x1, float y2) {
    x = x1;
    y = y2;
    randirx = random(-1, 1);
    randiry = random(-1, 1);
  }
  bacteria() {
    x = random(width);
    y = random(height);
    randirx = random(-1, 1);
    randiry = random(-1, 1);
  }
  void exist(bacteria other) {
    if (x>width|| x<0) {
      randirx*=-1;
    }
    if (y>height|| y<0) {
      randiry *= -1;
    }
    if(frameCount%100==0){
      other = new bacteria();
      bacterias.add(other);
    }
    strokeWeight(1);
    stroke(0);
    x += randirx;
    y += randiry;
    randirx += random(-0.01, 0.01);
    randiry += random(-0.01, 0.01);
    fill(50, 200, 50);
    ellipse(x, y, 20, 10);
    strokeWeight(4);
    stroke(255);
  }
}
//=========================================
class antibody {
  antibody() {
  }
  void exist() {
  }
}
//========================================
Sign In or Register to comment.