0

How can I achieve removing objects within an ArrayList. I tried the following code but the particles don't die. I outputted the size of the removeQue ArrayList and it outputs 1. The checkCollide method works fine for detecting collisions.

This is within a Class named Particles

  public void Update(){
    for(Enemies e: Enemies.enemies){
      if(this.checkCollide(e.x,e.y,e.mass)){
        removeQue.add(this);
      }
    }
    removeQue.clear(); // Remove objects
  }

More specifications:

Here is the whole class:

import java.util.ArrayList; import java.awt.*;

public class Particle{

  public static ArrayList<Particle> particles = new ArrayList<Particle>();
  public static ArrayList<Particle> removeQue = new ArrayList<Particle>();

  public static int particleCount;

  private int x,y,r,g,b,mass;

  private boolean playerParticle = false;

  private Color color = new Color((int)Math.floor(Math.random() * 256),(int)Math.floor(Math.random() * 256),(int)Math.floor(Math.random() * 256));

  public Particle(int x,int y, int mass, boolean p){
    particleCount++;
    this.x = x;
    this.y = y;
    this.mass = mass;
    playerParticle = p;
  }

  public void Update(){
    for(Enemies e: Enemies.enemies){
      if(this.checkCollide(e.x,e.y,e.mass) && !playerParticle){
        if(e.mass <= 200){
          e.addMass(this.mass);
        }
        if(e.mass >= 200){
          e.isTarget = false;
          e.goalReached = true;
          e.targetType = "c";
        }
        if(e.targetType.equals("p")){
          e.goalReached = true;
          e.isTarget = false;
        }
        this.x = (int) Math.floor(Math.random() * 10001);
        this.y = (int) Math.floor(Math.random() * 10001);
      }else if(this.checkCollide(e.x,e.y,e.mass) && playerParticle){
        e.addMass(this.mass);
        removeQue.add(this);
      }
    }
    removeQue.clear();
  }

  private boolean checkCollide(double x,double y,double mass){
    return x < this.x + 10 && x + mass > this.x && y < this.y + 10 && y + mass > this.y;
  }

  public void Draw(Graphics bbg){
    bbg.setColor(color);
    bbg.fillRect(x,y,10,10);
    bbg.drawRect(x,y,10,10);
  }


}

This is my main Update method

public void update(){
     for(Particle p: Particle.particles){
       p.Update();
     }

     for(Enemies e: Enemies.enemies){
       e.Update();
     }
   }
3
  • After your for loop you are calling clear(), how come it will output 1 for the size of removeQue? Commented Feb 21, 2016 at 19:26
  • I'm assuming it is because the contact between an enemy and a particle is continuous. Commented Feb 21, 2016 at 19:28
  • If you want remove a specific object, you can do removeQue.remove( Object o ), you can also remove using index if you know it by using removeQue.remove( index ) Commented Feb 21, 2016 at 19:30

1 Answer 1

1

I'm not sure if you're describing the situation correctly. After

removeQue.clear()

is called, the size of the ArrayList should be 0 since it is the last line of the method.

Sign up to request clarification or add additional context in comments.

8 Comments

So what happens is that as soon as an enemy comes in contact with a particle it starts printing one but the particle remains there. Prior to contact It outputs nothing.
Where exactly is the ArrayList "removeQue" declared?
Within the Particle class. Would you like me to post my whole class?
Is your main problem that you're trying to resolve the fact that removeQue's size does not become 0 after the Update method is called? Sure, I'll take a look at the whole class.
You don't have any code in the Update method that changes the particles ArrayList
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.