Ok so I'm not getting any errors with this code or anything, but there is a small problem. Basically I'm making an invaders game and to hold the different invaders I have an 2D array called invaders (all of the Invader objects extend Rectangle2D.Double). The laser object is basically just another Rectangle2D.Double that is fired by the player at the invaders. I have a timer firing every 85 milliseconds, and every time it fires it does the following check to see if there is a collision between the laser and any of the invaders; if there is it removes the laser and replaces the hit invader with a blank one. The problem is that the way I'm currently checking them causes the program to miss the collisions of some of the invaders because it goes through the invaders 2D array row by row checking for collisions. Can anyone tell me a more efficient way of checking the collisions all at the same time (or at least quicker) than going through row by row?
Here is the collision check:
if(laser != null && laser.hasFired) for(int row = 0;row < 5;row++) for(int col = 0;col < 11;col++) if(laser.getBounds2D().intersects(invaders[row][col].getBounds2D()) && invaders[row][col].alive){ laser.setFired(false); invaders[row][col].isAlive(false); score += 10; for(int x = 0;x < 5;x++) for(int y = 0;y < 11;y++) invaders[x][y].increaseSpeed(); }