0

I'm creating a simple space invaders game. I'm looking to delete one of the invaders once they are hit by a bullet. The invaders are made up of a 2D array of images and I've tested the collision between the image and the bullet (in an ArrayList) and that works fine. So the game detects a collision, the next step is to delete the correct object that has been hit. I'm a little confused as to how to correctly correspond where the bullet hits to which object it has hit in the 2D array, and then deleting it from the Array and carrying on with the game.

Below is how I created the invader array in setup()

  for(int i=0; i<2; i++){
    for(int j=0; j<4; j++){
        invArray[j][i]= new Taxi(taxiX, taxiY);
        taxiX= taxiX+ 100;
    }
    taxiX=20;
    taxiY= taxiY+ 140;
  }

I then filled the 2D Array with images in draw()

 for(int i=0; i<2; i++){
      for(int j=0; j<4; j++){
        invArray[j][i].update();
        if(invArray[j][i].y>=600){
           invArray[j][i].y= 0;
           invArray[j][i].render();
        }
      }
    }

2 Answers 2

0

You're using arrays which are fixed size. In theory you might be able to use array helper functions like shorten() and expand(), but you really got watch your counters and array structure. In practice, for a beginner, I would say this is error prone.

It might be simpler(but hackier) to set the array element of the hit invader to null, then simply check if the invader is not null before test collisions/rendering/etc.

e.g. in draw():

for(int i=0; i<2; i++){
      for(int j=0; j<4; j++){
        if(invArray[j][i] != null){
           invArray[j][i].update();
           if(invArray[j][i].y>=600){
              invArray[j][i].y= 0;
              invArray[j][i].render();
           }
         }
      }
    }

Another option is to use an ArrayList which has a dynamic size. e.g.

ArrayList<Taxi> invaders = new ArrayList<Taxi>();

In setup you'd do something similar:

for(int i=0; i<2; i++){
    for(int j=0; j<4; j++){
        invaders.add(new Taxi(taxiX, taxiY));
        taxiX= taxiX+ 100;
    }
    taxiX=20;
    taxiY= taxiY+ 140;
  }

then in draw():

for(int i = 0 ; i < invaders.size(); i++){
    Taxi t = invaders.get(i);
    t.update();
    if(t.y>=600){
       t.y= 0;
       t.render();
    }
    /*
    if(YOUR_HIT_CONDITION_HERE){
        invaders.remove(t);
    }
    */
}

It's a bit tricky to go back and forth between 1D and 2D arrays/indexing at the beginning, but it's not that bad once you get the hand of it.

To convert from 2D x,y to 1D index:

int index = x + y * width;

(where x,y are you counters and width is the width of your grid (number of columns)).

The other way around, 1D index to 2D x,y:

int x = index % width; int y = index / width;

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

4 Comments

I was thinking of switching over to an ArrayList but wasn't sure how to handle to two rows but I'll try it. Thank you so much!
Feel free to vote and/or mark the answer if it was helpful ;)
I've changed everything to an ArrayList now thanks for that :) However the hit detection is off, sometimes it registers and sometimes it does not. For example, the bullets go through the first line of invaders and then hit the second row (the back row). The remove method works for a couple of invaders, after that I'm getting an IndexOutOfBoundsException error for some reason.
It depends how you do the collision detection. It's unclear if the same bullet goes through the first invader, then the second and if it should do that in the first place. I recommend asking a separate question, detailing this aspect
0

Try to decouple the hit detection from removing elements from the arraylist, maybe using a flag and removing at the end on the draw loop. Use arraylist.size() as limit of the loop in one part of the code. Maybe that can solve your problem with hit detection, maybe you need a counter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.