Problems with objects updating

Hi, I was creating a game and so far everything was going well until I decided to change the way to make trails which I had in my game and ever since theirs been a problem with a few of them rendering at all.

int level=0,plx,ply,timer=1000,plydmg=0;
float hbar,health=150,maxhealth,heal;

boolean[] keys = new boolean[4];
boolean spawning = true,alive = true,healing = false,dmganim = false;

color plycolor = color(250);

ArrayList<BasicEnemy> benemys = new ArrayList<BasicEnemy>();
ArrayList<Trail> trails = new ArrayList<Trail>();

Level lvl;

void setup() {
  size(800,600);
  for (int i=0; i < 4; i++) keys[i] = false;
  fill(250);
  noStroke();
  plx = width/2-25;
  ply = height/2-25;
  rect(plx,ply,50,50);
  lvl = new Level();
  maxhealth = health;
}

void draw() {
  background(0);
  if (health <= 0) alive = false;
  float dmgcheck = health;
  noStroke();
  for (int i=0; i < trails.size(); i++) trails.get(i).update();
  for (int i=0; i < benemys.size(); i++) benemys.get(i).update();
if (alive == true) {
    if (keyPressed) {
      if (keys[0] && ply > 0) ply -= 7;
      if (keys[1] && ply < height-51) ply += 7;
      if (keys[2] && plx < width-52) plx += 7;
      if (keys[3] && plx > 0) plx -= 7;
    }
    if (timer <= 0) {
      lvl.clean();
      lvl = new Level();
    } else timer--;
  lvl.handler();
    
    if (health < dmgcheck && dmganim == false) {
      plycolor = color(250,75,75);
      dmganim = true;
    }
    if (dmganim == true) {
      if (green(plycolor) < 150) plycolor = color(250,green(plycolor)+10,blue(plycolor)+10); else {
        if (plydmg < 2) {
          plycolor = color(250,75,75);
          plydmg++;
        } else {
          plycolor = color(250);
          plydmg = 0;
          dmganim = false;
        }
      }
    }
    fill(plycolor);
    trails.add(new Trail(plx,ply,50,0.1,35,plycolor));
    noStroke();
    rect(plx,ply,50,50);
    
    if (health > maxhealth) maxhealth = health;
    fill(100);
    rect(30,20,100,20);
    fill(255,0,0);
    hbar = map(health,0,maxhealth,0,94);
    if (health > 0) rect(33,23,hbar,14);
    fill(250);
    textSize(20);
    text("Level "+level,width-100,30);
  } else {
    fill(0);
    textSize(100);
    text("Game Over",width/6,height/2+20);
    fill(200,0,0);
    textSize(99);
    text("Game Over",width/6+3,height/2+19);
  }
  if (healing == true) {
    if (heal > 0 && health < maxhealth) {
      health += 0.5;
      heal -= 0.5;
    } else healing = false;
  }
}

void keyPressed() {
  if (alive == true) {
  if (key == 'w' || key == 'W')
     keys[0]=true;
   if (key == 's' || key == 'S')
     keys[1]=true;
     if (key == 'd' || key == 'D')
     keys[2]=true;
   if (key == 'a' || key == 'A')
     keys[3]=true;
  }
}

void keyReleased() {
  if (key == 'w' || key == 'W')
     keys[0]=false;
   if (key == 's' || key == 'S')
     keys[1]=false;
     if (key == 'd' || key == 'D')
     keys[2]=false;
   if (key == 'a' || key == 'A')
     keys[3]=false;
}

class BasicEnemy {
  float x,y,speed,damage;
  int size,timer=10,xdir,ydir;
  color c = color(255,0,0);
  BasicEnemy(float tempX, float tempY, int tempXdir, int tempYdir, int tempSize, float tempSpeed, float tempDamage) {
    x = tempX;
    y = tempY;
    xdir = tempXdir;
    ydir = tempYdir;
    size = tempSize;
    speed = tempSpeed;
    damage = tempDamage;
  }
  void update() {
    x = x + speed * xdir;
    y = y + speed * ydir;
    if (timer <= 0) {
      timer = 10;
      trails.add(new Trail(x,y,size,0.1,5,c)); //0.2,10
    } else timer--;
    fill(c);
    rect(x,y,size,size);
    if (x+size > plx && x < plx+50 && y+size > ply && y < ply+50) health -= damage;
    //if (x > width || x < 0-size || y > height || y < 0-size) benemys.remove(this);
  }
  void kill() {
    benemys.remove(this);
  }
  void setColor(color tempColor) {
    c = tempColor;
  }
}

class Trail {
  float x,y,life,drain,fade,r,g,b;
  int size;
  Trail(float tempX, float tempY, int tempSize, float tempLife, float tempFade, color tempCol) {
    x = tempX;
    y = tempY;
    size = tempSize;
    life = tempLife;
    drain = tempLife;
    fade = tempFade;
    r = red(tempCol);
    g = green(tempCol);
    b = blue(tempCol);
  }
  void update() {
    if (r > 0 || g > 0 || b > 0) {
      fill(r,g,b);
      rect(x,y,size,size);
      life-=0.1;
      if (life < 0) {
        if (r > 0) r-=fade;
        if (g > 0) g-=fade;
        if (b > 0) b-=fade;
        life = drain;
      }
    } else trails.remove(this);
  }
}

class Level {
  int time=20,laytimer=0,layer=0;
  Level() {
    level++;
    spawning = true;
  }
  void handler() {
    if (level == 1 && spawning == true) {
      spawning = false;
      timer = 500;
      benemys.add(new BasicEnemy(-10,100,1,0,25,2,1));
      benemys.add(new BasicEnemy(-10,200,1,0,25,2,1));
      benemys.add(new BasicEnemy(-10,300,1,0,25,2,1));
      benemys.add(new BasicEnemy(-10,400,1,0,25,2,1));
      benemys.add(new BasicEnemy(-10,500,1,0,25,2,1));
      benemys.add(new BasicEnemy(width+10,50,-1,0,25,2,1));
      benemys.add(new BasicEnemy(width+10,150,-1,0,25,2,1));
      benemys.add(new BasicEnemy(width+10,250,-1,0,25,2,1));
      benemys.add(new BasicEnemy(width+10,350,-1,0,25,2,1));
      benemys.add(new BasicEnemy(width+10,450,-1,0,25,2,1));
      benemys.add(new BasicEnemy(width+10,550,-1,0,25,2,1));
    } else if (level == 2 && spawning == true) {
      spawning = false;
      timer = 500;
      benemys.add(new BasicEnemy(-10,100,1,0,25,2,1));
      benemys.add(new BasicEnemy(-10,200,1,0,25,2,1));
      benemys.add(new BasicEnemy(-10,300,1,0,25,2,1));
      benemys.add(new BasicEnemy(-10,400,1,0,25,2,1));
      benemys.add(new BasicEnemy(-10,500,1,0,25,2,1));
      benemys.add(new BasicEnemy(width+10,100,-1,0,25,2,1));
      benemys.add(new BasicEnemy(width+10,200,-1,0,25,2,1));
      benemys.add(new BasicEnemy(width+10,300,-1,0,25,2,1));
      benemys.add(new BasicEnemy(width+10,400,-1,0,25,2,1));
      benemys.add(new BasicEnemy(width+10,500,-1,0,25,2,1));
      benemys.add(new BasicEnemy(150,-10,0,1,25,2,1));
      benemys.add(new BasicEnemy(300,-10,0,1,25,2,1));
      benemys.add(new BasicEnemy(450,-10,0,1,25,2,1));
      benemys.add(new BasicEnemy(600,-10,0,1,25,2,1));
      benemys.add(new BasicEnemy(750,-10,0,1,25,2,1));
      benemys.add(new BasicEnemy(100,height+10,0,-1,25,2,1));
      benemys.add(new BasicEnemy(200,height+10,0,-1,25,2,1));
      benemys.add(new BasicEnemy(300,height+10,0,-1,25,2,1));
      benemys.add(new BasicEnemy(400,height+10,0,-1,25,2,1));
      benemys.add(new BasicEnemy(500,height+10,0,-1,25,2,1));
      benemys.add(new BasicEnemy(600,height+10,0,-1,25,2,1));
      benemys.add(new BasicEnemy(700,height+10,0,-1,25,2,1));
    }
  }
void clean() {
    benemys.clear();
    //trails.clear();
  }
  void end() {
    spawning = false;
    layer = 0;
    laytimer = 0;
  }
}

This is the code to get the first couple of the levels working so basically you can see that it makes some of the trails flicker and with some of my enemies it just doesn’t even place a trail.

1 Like

What is this doing? Can you explain it?

sorry for the late reply but that checks for when the rgb value is <= 0 so when it’s black and then it removes it from the array list because when it’s black it blends in the background so it’s more efficient to remove it from the list.

One thing I noticed: in draw you loop over trails.size and call update. In update, you remove items from trails, changing its size. This could cause many subtle or dramatic bugs most commonly because not every object is visited every frame (each delete shifts the next index forward, skipping it).

1 Like