Java – The ArrayList object was not deleted correctly

The ArrayList object was not deleted correctly… here is a solution to the problem.

The ArrayList object was not deleted correctly

I’m working on a 2D platformer for the last project of my HS school year.
The game is basically about a player walking back and forth, collecting points and reaching goals… The player can fire a bullet, and when the bullet hits a square block, it is destroyed. Now I want to add an explosion effect using what is known as a “particle” object. I’ve written the manager class for it and it seems to work for the first time, but after shooting a few times, I notice that the particles are no longer deleted, they just continue and leave the screen. The lifetime limit is 500ns.

I

also noticed that if I fired a bullet immediately at the beginning of the game, the effect ended as expected. But after waiting a few more seconds and then firing the bullet, the effect particles are not functioning properly.

This is what I looked like when I fired bullets at the beginning of the game (it was supposed to look like this):
enter image description here

This is what it looks like, wait a few seconds before firing a bullet.
enter image description here

The Particle Manager .java

public class ParticleManager {

private ArrayList<Particle> particles;
    private ArrayList<Particle> removeParticles;

public ParticleManager() {
        particles = new ArrayList<Particle>();
        removeParticles = new ArrayList<Particle>();
    }

public int getListSize() {
        return particles.size();
    }

/*
            Generate particles
     */
    public void genParticle(int x, int y, int amount) {
        for(int i = 0; i < amount; i++) {
            particles.add(new Particle("explosion" , x,y, i));
        }
    }

public void update() {

 Iterate trough particle objects
         update them & check for lifeTime
        for(Particle p: particles) {

 Updating particle object before 
             checking for time lapse
            p.update();

 Append outdated particles to removeParticles
             if time limit has passed
            if(System.nanoTime() - p.timePassed >= Config.particleLife) {
                removeParticles.add(p);
            }
        }

 finally, delete all "remove-marked" objects
        particles.removeAll(removeParticles);
    }

public void render(Graphics2D g) {
        for(Particle p: particles) {
            p.render(g);
        }
    }

}

Particle .java

class Particle {

private double px, py, x, y; 
    private int radius, angle;
    public long timePassed;
    String type;

public Particle(String type, double x, double y, int angle) {
        this.x = x;
        this.y = y;
        this.radius = 0;
        this.angle = angle; 
        this.timePassed = 0;
        this.type = type;  explosion, tail

}

public void update() {
        px  =   x + radius * Math.cos(angle);
        py  =   y + radius * Math.sin(angle);
        radius += 2;

this.timePassed = System.nanoTime();
    }

public void render(Graphics2D g) {
        g.setColor(Color.WHITE);
        g.fillOval((int)px, (int)py, 5, 5); 
    }
}

I

haven’t figured out what I’m doing wrong here, I googled something and once I came across an answer mentioning that some quotes weren’t removed directly for some reason….

My question is, “How do you make these particles disappear after a certain amount of time?” – As shown in the first GIF”

Solution

I think the problem is that you constantly override timePassed.

// Updating particle object before 
 checking for time lapse
p.update();

 Append outdated particles to removeParticles
 if time limit has passed
if(System.nanoTime() - p.timePassed >= Config.particleLife) {
    removeParticles.add(p);
}

p.update() sets timePassed to now, and then if checks to check if the time is far away from now (it never because it was just set).

I think you do want to set timePassed in the constructor (maybe it would be better to name it timeCreated).

Also, note that you will never clear removeParticles, so the list will grow forever until it causes the process to run out of memory.

Related Problems and Solutions