3 Replies Latest reply: Nov 25, 2009 10:47 AM by 796262 RSS

    How do i remove an Object (aliens/ bullets) from my Space Invaders game??

    843853
      Creating Space Invaders game and im getting confused on removing objects from my game. How do you actually remove an object (i.e bullets and aliens).

      I know this isnt the right way to do it, but for the bullets i had an array of the Objects bullets[] and set the x and y coordinates off screen so u cant see then wen they are painted you cant see them, then wen the player fires them, the x and y coordinates change so they come from the players ship till it hits an alien or goes off screen, then i reset the coordinates so they are pasted off screen again. Im relatively new to this so i would appreciate any help. Im going to have to do the same with aliens wen a collision is detected. Is there a way of actually removing an object?

      Here is sum code incase you need to see it:
      public void checkCollision()
           {
                Rectangle playerRec = new Rectangle(player1.getBoundingBox());
                
                
                for(int i=0; i<alienRow; i++)
                     for(int j=0; j<alienCol; j++){
                     {
                          if(playerRec.intersects(aliens[i][j].getBoundingBox()))
                          {
                               collisionDetected();
                          }
                          for(int k=0; k<bulletNum; k++){
                               Rectangle bulletRec = new Rectangle(bullets[k].getBoundingBox());
                               if(bulletRec.intersects(aliens[i][j].getBoundingBox()))
                               {
                                    removeBullet(bullets[k]);
                                    collisionDetected();
                               }
                          }
                     }
                }
           }
           public static void collisionDetected()
           {
                System.out.println("COLLISION");
           }
      private void removeBullet(Bullets bullet){
                bullet.fired=false;
                bullet.setX(-10);
                bullet.setY(-10);
           }
      Edited by: deathwings on Nov 25, 2009 8:20 AM
        • 1. Re: How do i remove an Object (aliens/ bullets) from my Space Invaders game??
          796262
          deathwings wrote:
          Creating Space Invaders game and im getting confused on removing objects from my game. How do you actually remove an object (i.e bullets and aliens).
          I would use an [ArrayList |http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html] that holds the active aliens or bullets. When you want to remove them, simply call the remove( ) function. Then in your paintComponent method (which I don't see here), just draw any aliens or bullets in the ArrayList.

          Alternatively, you could use your arrays and just set the aliens or bullets to null when you don't want them around anymore. Just make sure whenever you're looping through your arrays that you check for null then.

          Yet another option is to have a boolean value inside your aliens and bullet classes that keeps track of whether or not that object is active.

          I'd go with the first option though.
          • 2. Re: How do i remove an Object (aliens/ bullets) from my Space Invaders game??
            843853
            I was thinking bout that arraylist angle before, but it makes sense now so i think that will work ok. Thx kevin.
            • 3. Re: How do i remove an Object (aliens/ bullets) from my Space Invaders game??
              796262
              deathwings wrote:
              I was thinking bout that arraylist angle before, but it makes sense now so i think that will work ok. Thx kevin.
              Not a problem.

              Taking the List idea one step further, you could have a parent Object/Interface that all of your game objects extend/implement. Let's call it GameObject.

              A GameObject would have a draw(Graphics g) function that you call from your paintComponent( ) method, and for example an act( ) function as well.

              You'd have a single List of GameObjects that you add everything in your game into. Now painting is simply a matter of looping over a single List. And in your game logic loop, you could do a similar thing with the act( ) function: your bullet's act( ) would simply move it forward and check for collisions and going off screen. An alien's act( ) function would move it around and maybe shoot a weapon every so often.

              Just an idea.