ArrayList Problems / get() & remove()

Hi everyone,

I am a newbie in processing, at my early stages of learning and playing with this amazing language. I am trying to create a kind of “space invader” program from scratch.

I don’t want to get inspired by the real one, just trying to make my own version to challenge myself.

I have divided my reflexion into smaller manageable parts and now i am stucked. I am currently working on the “bullets part”. The idea is to create two shots (controlled by mouseX & mouseY) every time that mousePressed is activated.

I am using an ArrayList, and i can’t understand why but it says that the functions get() & remove() do not exist.

Can someone help me on this one please?

Thank you,

Rémy


ArrayList<Bullet> bullets;

void setup(){
  size(400,400);
  smooth();
  
  bullets = new ArrayList<Bullet>();
  
  bullets.add(new Bullet(mouseX,mouseY));
  
}


void display(){
  background(255);
  
  for (int i = bullets.size()-1; i >= 0; i--) { 
  Bullet bullets = bullets.get(i);
  bullets.move();
  bullets.display();
  
  if (bullets.finished()){
    bullets.remove(i);
  }
 }
}

void mousePressed(){
  bullets.add(new Bullet(mouseX,mouseY));
}

class Bullet{
  
  float bulletX1, bulletY1;
  float bulletX2, bulletY2;
  int r;
  int bulletSpeed;
  
  Bullet(float _x, float _y){
    
    bulletX1 = _x - 10;
    bulletY1 = _y;
    bulletX2 = _x +10;
    bulletY2 = _y;
    r = 6;
    bulletSpeed = 1;
  
  }
  
  void move(){
    bulletY1 -= bulletSpeed;
    bulletY2 -= bulletSpeed;
  }
  
    boolean finished() {
    if ((bulletY1<0) || (bulletY2<0)) {
      return true;
    } else {
      return false;
    }
  }
  
  void display(){
    fill(255,0,0);
    noStroke();
    ellipse (bulletX1,bulletY1,r,r);
    ellipse (bulletX2,bulletY2,r,r);  
  }
}

1 Like

Don’t use the same name for your variable

  • ArrayList<Bullet> bullets;
    and
  • Bullet bullets = bullets.get(i);

Say (for example) :

  • Bullet b = bullets.get(i);

By the way for ArrayLists you can do this instead :

for(Bullet b : bullets){
    //Your code : b.move()...
}
3 Likes

Hi @josephh,

Thank you for your message. Sorry for asking but why do i need to change the name? I really want to make sure that i understand well the logic behind.

Also, i just changed the variable name but the screen is grey and this is still not working.
I changed the “size” position cause i had a message error when it was packed in setup()

Best,

Rémy

ArrayList<Bullet> bullets;

void settings(){
   size(400,400);
}

void setup(){

  smooth();
  
  bullets = new ArrayList<Bullet>();
  
  bullets.add(new Bullet(mouseX,mouseY));
  
}


void display(){
  background(255);
  
  for (int i = bullets.size()-1; i >= 0; i--) { 
  Bullet b = bullets.get(i);
  b.move();
  b.display();
  
  if (b.finished()){
    bullets.remove(i);
  }
 }
}

void mousePressed(){
  bullets.add(new Bullet(mouseX,mouseY));
}

class Bullet{
  
  float bulletX1, bulletY1;
  float bulletX2, bulletY2;
  int r;
  int bulletSpeed;
  
  Bullet(float _x, float _y){
    
    bulletX1 = _x - 10;
    bulletY1 = _y;
    bulletX2 = _x +10;
    bulletY2 = _y;
    r = 6;
    bulletSpeed = 1;
  
  }
  
  void move(){
    bulletY1 -= bulletSpeed;
    bulletY2 -= bulletSpeed;
  }
  
    boolean finished() {
    if ((bulletY1<0) || (bulletY2<0)) {
      return true;
    } else {
      return false;
    }
  }
  
  
  void display(){
    fill(255,0,0);
    noStroke();
    ellipse (bulletX1,bulletY1,r,r);
    ellipse (bulletX2,bulletY2,r,r);  
  }
  

}

Because two variables mustn’t have the same name.

The reason if that you have to call the function :

void draw(){

}

instead of :

void display(){

}
4 Likes

Awesome. Of course! I didn’t saw it.

Thank you @josephh for the advices.

Best,

Rémy

2 Likes

Regarding why that specific error: As @josephh pointed out, you first name your ArrayList bullets and then, in your for loop, you give a single bullet the same name – bullets. So, your error that get does not exist is because bullets is now of class Bullet, not of class ArrayList, and class Bullet doesn’t have a get() method.

Two variables in different scope may have the same name – or you may redefine a variable name to point to a different object. However, redefining names can often lead to confusion.

  1. “When I say Big Red, I mean my dog.”
  2. “When I say Big Red, I mean my car.”
  3. “Please feed Big Red.”
    “…how do you feed a car?” ( method feed does not exist for object of class Car )
1 Like

Hi @jeremydouglass,

Thank you for your explanation, it makes totally sens!

By the way, i am pretty impressed by the help and reactivity of the community.

You guys are the best!

Rémy

1 Like

Hi Everyone,

So my code is progressing well. I am looking for a way to erase the aliens when bullets touch them.
I am a bit lost regarding the boolean variable using the distance between the bullets and the round body of the aliens.

I want to kill the aliens when a bullet hits them, erase them from the screen and from the list.

A solution for it?

Thank you,

Rémy


Alien[]aliens = new Alien[1000];
SpaceShip spaceship;
ArrayList<Bullet> bullets;
Timer timer;

int totalAliens = 0;

void setup(){
  
  size(600,400);
  smooth();
  frameRate(60);
  
  timer = new Timer(1000);
  timer.start();
  
  spaceship = new SpaceShip(10);
  
    
  bullets = new ArrayList<Bullet>();
  bullets.add(new Bullet(mouseX,mouseY));
  
}

void draw(){
  
  background(255);
  
    if(timer.isFinished()){
       aliens[totalAliens] = new Alien();
       totalAliens++;
       if (totalAliens >= aliens.length){
         totalAliens = 0;
       }
      timer.start();
    }
  
  for (int i = bullets.size()-1; i >= 0; i--) { 
  Bullet b = bullets.get(i);
  b.move();
  b.display();
  
  if (b.finished()){
    bullets.remove(i);
  }
 }
  
  
  for (int i = 0; i < totalAliens ; i++){
  aliens[i].move();
  aliens[i].display();
  }
  
  spaceship.move(mouseX, mouseY);
  spaceship.display();

}


void mousePressed(){
  bullets.add(new Bullet(mouseX,mouseY));
}

class Timer{

  int savedTime;
  int totalTime;

  Timer(int _tempTotalTime){
    totalTime = _tempTotalTime; 
  }

  void start(){
    savedTime = millis();
  }

  boolean isFinished(){
    int passedTime = millis() - savedTime;
    if(passedTime > totalTime){
      return true;
    } else {
      return false;
    }
  }
}

class Alien {
  
  float x;
  float y;
  float diam;
  float speed;
  
  Alien(){
    x = random(width);
    y = random(-100,-50);
    diam = random(20,40);
    speed = diam*0.1;
  }
  
  
  void move(){
    y += speed;
  }
  
  
  void display(){
    
    stroke(0);
    strokeWeight(diam*0.1);

    ellipseMode(CENTER);
    noFill();
    arc(x, y + diam*0.5, 2*diam, diam, -PI, 0);
  
    arc (x-diam,y+diam*0.75,diam*0.5,diam*0.5,-PI,0);
    arc (x+diam,y+diam*0.75,diam*0.5,diam*0.5,-PI,0);
  
    fill(200);
    ellipse(x,y,diam,diam);
  }
  
  boolean reachedBottom(){
    if (y > height + diam*4){
      return true;
    } else {
      return false;
    }
  }
  
}


class Bullet{
  
  float bulletX1, bulletY1;
  float bulletX2, bulletY2;
  int r;
  int bulletSpeed;
  
  Bullet(float _x, float _y){
    
    bulletX1 = _x - 20;
    bulletY1 = _y - 15;
    bulletX2 = _x + 20;
    bulletY2 = _y - 15;
    r = 6;
    bulletSpeed = 2;
  
  }
  
  void move(){
    bulletY1 -= bulletSpeed;
    bulletY2 -= bulletSpeed;
  }
  
    boolean finished() {
    if ((bulletY1<0) || (bulletY2<0)) {
      return true;
    } else {
      return false;
    }
  }
  
  void display(){
    fill(255,0,0);
    noStroke();
    ellipse (bulletX1,bulletY1,r,r);
    ellipse (bulletX2,bulletY2,r,r);  
  }
  
   boolean intersect(Alien d){
   float distance = dist(bulletX1,bulletY1,d.x,d.y);
  if (distance < r + d.diam){
    return true;
  } else {
    return false;
  }
}
  

}


class SpaceShip{
  
float x;
float y;
int shipRatio;

 SpaceShip(int tempShipRatio){
   x = width/2;
   y = height/2;
   shipRatio = tempShipRatio;
 }
 
 void move (float tempX, float tempY){
   x = tempX;
   y = tempY + shipRatio*2;;
   
 }
 
 void display(){
  stroke(0);
  strokeWeight (3);
  fill(210);
  
  line (x - shipRatio*2, y - shipRatio, x - shipRatio*2, y - shipRatio*4);
  line (x + shipRatio*2, y - shipRatio, x + shipRatio*2, y - shipRatio*4);
  
  triangle (x - shipRatio*3, y - shipRatio, x, y - shipRatio*4, x + shipRatio*3, y-shipRatio);
  
  }
}



It’s going to be easy to remove the bullets that hit from your ArrayList because an ArrayList is specifically designed to allow you to remove elements from it.

Removing a single Alien from an Array of Aliens is not going to be as easy.

So… why are you using an Array of Aliens, instead of an ArrayList of them? You don’t actually need space for 1000 aliens at a time. Just a few that are on the screen.

Anyway, I’m getting ahead of myself. You want to do collisions. You need to check - for each alien - if any bullet is hitting it.

Thus you will first need to loop over every alien.

for ( each alien ){

Then you will need to loop ove each bullet.

  for( each bullet ) {

Then you need to see if those collide.

if( alien.collides( bullet ) ){

If so, you remove that alien and that bullet.


When is a round bullet colliding with a round alien? Basically, when are two circles intersecting? What can you say about the distance between their centers in relationship to the sum of their radii?


You might see this thread I posted recently: