Java - ArrayList 对象未正确删除

标签 java nanotime

我正在为我 HS 学年的最后一个项目开发一款 2D 平台游戏。 游戏基本上是关于一个玩家来回走动、收集点数并达到目标……玩家可以发射子弹,当子弹击中一个方 block 时,它就会被摧毁。现在,我想使用所谓的“粒子”对象添加爆炸效果。我已经为它编写了管理器类,它似乎是第一次工作,但在拍摄了几次之后,我注意到粒子不再被删除,它们只是继续并离开屏幕。生命周期限制为 500ns。

我还注意到,如果我在游戏开始时立即发射子弹,效果就会按预期结束。但是再等几秒钟然后发射子弹后,效果粒子就没有正常运行了。

这是我一开始游戏就发射子弹时的样子(它应该是这样的):
enter image description here

这是它的样子,在发射子弹之前等待几秒钟。 enter image description here

粒子管理器.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);
        }
    }

}

粒子.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); 
    }
}

我还没有弄清楚我在这里做错了什么,我用谷歌搜索了一些东西,有一次我遇到了一个答案,提到一些引用由于某种原因没有被直接删除......

我的问题是“如何让这些粒子在经过一定时间后消失?- 如第一个 GIF 所示”

最佳答案

我认为问题在于您不断地覆盖 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()timePassed 设置为现在,然后 if 检查检查时间过去是否离现在很远(它永远不会因为它刚刚设置) .

我认为您确实想在构造函数中设置 timePassed(也许将它命名为 timeCreated 会更好)。

此外,请注意,您永远不会清除 removeParticles,因此该列表将永远增长,直到它导致进程耗尽内存。

关于Java - ArrayList 对象未正确删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52239112/

相关文章:

java - OSGI 容器中的数据源

php - 如何以纳秒精度获得 PHP 时间?

android - SensorEvent.timestamp 不一致

java - fork /加入 : Collecting results

java - Eclipse Kepler 在某个工作区崩溃

java - 如果某些值不存在则插入到表中

java - 在 Primefaces 中拉伸(stretch)列表

android - 以纳秒为单位测量纪元时间 - Android

java - strace Java 小程序

java - 如何暂停 Java 线程一小段时间,比如 100 纳秒?