Loading...
Logo
Processing Forum
Code below.

Simple question. How does one remove an object from an Array List so that it isn't in the game anymore? This would mean it only affects that individual object of the list, and doesn't just become invisible so it no longer affects the game.

Copy code
  1.  class Missile {
          float endX;    // End X position
          float endY;    // End Y position
          float endvarX;  // Line gradient X
          float endvarY; // Line gradient Y
          float startX;  // Start of missile line
          float startY;  // start of missile line

          color c;
          Missile(float init_endvarX, float init_endvarY, float init_startX, float init_startY,
          color init_c) 
          {
            endX = init_startX;
            endY = init_startY;
            endvarX = init_endvarX;
            endvarY = init_endvarY;
            startX = init_startX;
            startY = init_startY;
            c = init_c;
          }

          void render() {
            {
            smooth();
            stroke(c);
            line(startX, startY, endX, endY);
            }
          }

          void update() {
            endY = endY + endvarY;
            endX = endX + endvarX;
           
            if (dist(endX, endY, posX, posY) <= 15){
            background(255,0,0);
          } else{
          }
          }
         
         
        } // class Missile
  2.  
     int framestore; // frame last missile was created on
     int Mdelay;     //delay between next fire time
     int posX; //mouse X position of player
     int posY; //mouse Y position of player
     float fireX; //starting X co-ordinate of player missile
     int fireY; //starting Y co-ordinate of player missile

       
        Missile randomMissile() {
          return new Missile(random(-2.0, 2.0), random(0.5, 2.5),
          random (200, 600), 0,
          color(random(0, 256),
          random(0, 256),
          random(0, 256))
            );
        }
       
        ArrayList<Missile> missilelist; // list in null initially

        void setup() {
          size(700, 500);
          framestore = 0;
          Mdelay = (int)random(60, 150);
          missilelist = new ArrayList<Missile>(); // creates and initially empty ArrayList of missile objects
         
          frameRate(30);
           
        }

        void draw() {
          background(255);
          if (Mdelay < (frameCount - framestore) ) {
            for (int i = 0; i < (int)random(3); i = i+1) { // gives the initial value of 0
            // then randomized between 3 maximum values. This means there can only be between 1 and
            // 3 missiles active
            // then adds +1 to the value of i, but cannot be more than 3.
            // code courtesy of Jacques
              missilelist.add(randomMissile());
            } 
            framestore = frameCount;
          }
         
          // render all the missiles.
          for (Missile b : missilelist) {
            b.update();
            b.render();
      
        
          }
          fill(255, 80, 80);
          triangle(350, 480, 335, 500, 365, 500);
         
         
         
        }
       
        void mousePressed() {
        fireX = 350;
        fireY = 480;
        posX = mouseX;
        posY = mouseY;
       
        smooth();
        strokeWeight(2.5);
        stroke(255,0,0);
        line(fireX, fireY, posX, posY);
        }

Replies(7)

Copy code
  1. ArrayList.remove(int index)
or
Copy code
  1. ArrayList.remove(Object object)
Could you give me an example of this? This doesn't seem to work for me.
I'm not quite sure where you want to remove the item from the ArrayList... The ArrayList in question would probably be missilelist.
I used this bit of code here:

missilelist.remove(randomMissile());

But nothing happens in the program, no objects are being removed.

EDIT: I'm using an 'if' statement in the update function to create a condition for it to be removed. The if statement works because I've tested it with other functions.

Full game below:

Copy code
  1.     class Missile {
          float endX;    // End X position
          float endY;    // End Y position
          float endvarX;  // Line gradient X
          float endvarY; // Line gradient Y
          float startX;  // Start of missile line
          float startY;  // start of missile line

          color c;
          Missile(float init_endvarX, float init_endvarY, float init_startX, float init_startY,
          color init_c) 
          {
            endX = init_startX;
            endY = init_startY;
            endvarX = init_endvarX;
            endvarY = init_endvarY;
            startX = init_startX;
            startY = init_startY;
            c = init_c;
          }

          void render() {
            {
            smooth();
            stroke(c);
            line(startX, startY, endX, endY);
            }
          }

          void update() {
            endY = endY + endvarY;
            endX = endX + endvarX;
           
            if (dist(endX, endY, posX, posY) <= 15){
            missilelist.remove(randomMissile());
          } else{
          }
          }
         
         
        } // class Missile
  2.  
     int framestore; // frame last missile was created on
     int Mdelay;     //delay between next fire time
     int posX; //mouse X position of player
     int posY; //mouse Y position of player
     float fireX; //starting X co-ordinate of player missile
     int fireY; //starting Y co-ordinate of player missile

       
        Missile randomMissile() {
          return new Missile(random(-2.0, 2.0), random(0.5, 2.5),
          random (200, 600), 0,
          color(random(0, 256),
          random(0, 256),
          random(0, 256))
            );
        }
       
        ArrayList<Missile> missilelist; // list in null initially

        void setup() {
          size(700, 500);
          framestore = 0;
          Mdelay = (int)random(60, 150);
          missilelist = new ArrayList<Missile>(); // creates and initially empty ArrayList of missile objects
          frameRate(30);
           
        }

        void draw() {
          background(255);
          if (Mdelay < (frameCount - framestore) ) {
            for (int i = 0; i < (int)random(3); i = i+1) { // gives the initial value of 0
            // then randomized between 3 maximum values. This means there can only be between 1 and
            // 3 missiles active
            // then adds +1 to the value of i, but cannot be more than 3.
            // code courtesy of Jacques
              missilelist.add(randomMissile());
            } 
            framestore = frameCount;
          }
         
          // render all the missiles.
          for (Missile b : missilelist) {
            b.update();
            b.render();
      
        
          }
          fill(255, 80, 80);
          triangle(350, 480, 335, 500, 365, 500);
         
         
         
        }
       
        void mousePressed() {
        fireX = 350;
        fireY = 480;
        posX = mouseX;
        posY = mouseY;
       
        smooth();
        strokeWeight(2.5);
        stroke(255,0,0);
        line(fireX, fireY, posX, posY);
        }

remove() removes an object in the list. randomMissile() creates a new missile object, thus it won't be in the list. If you want to remove a random missile that is in the list, then pick a random int between 0 and the list's size.
I'm still totally stuck. I don't know what to do with my code at all.
Actually, you should move the collision test to draw.
      for (Missile b : missilelist) {
        b.update();
        b.render();
        if (dist(b.endX, b.endY, posX, posY) <= 15){
        missilelist.remove(b);
      }
      }
(untested)
Hint: Ctrl+T (or Edit > Auto Format) helps in getting a good vision of your code.