So, I was assigned to complete my own mod for Space Invaders by my teacher. I chose to create different types of laser bases for the user. Each laserbase can shoot different missiles. Currently I am stuck on how the shield will react to each missile. I have a "Machine gun" type of laser base that shoots 2x5 FilledRect's at a shield that is just a matrix of FilledRects side by side, each being 2x5. HOWEVER, when I get to my normal laserbase, which shoots missiles which are 2x10 FilledRect's, i have a problem. I try to just remove 2 filled rect's at the same time but i just get an array out of bounds error. here is the code:
when a single shield is hit, (the .i****(FilledRect projectile) ) method, it just hides the projectile, as well as hides the shield piece which is the filledrect
thank you!!!
Code:
public class CompleteShield extends ActiveObject { /* * constants */ private static final int COLUMNS = SpaceInvaders.RIGHT_BORDER / 16; private static final int ROWS = 6; private static final int FIRST_SHIELD_X = SpaceInvaders.RIGHT_BORDER / 8; // X coord for the first shield private static final int SECOND_SHIELD_X = FIRST_SHIELD_X + COLUMNS + 100; // X coord for the second shield private static final int THIRD_SHIELD_X = SECOND_SHIELD_X + COLUMNS + 100; // X coord for the third shield private static final int SHIELD_Y = SpaceInvaders.BOTTOM_BORDER - 80; // Y coord for all shields /* * instance fields */ private Shield[][] shield1; // the matrix containing the first shield pieces private Shield[][] shield2; // the matrix containing the second shield pieces private Shield[][] shield3; // the matrix containing the third shield pieces private LaserBase theBase; // the laser base in the program /* * class fields */ /** * Invaders constructor with images for the different alien views as well as one * being hit. * * @param canvas the canvas in which the image is created */ public CompleteShield (DrawingCanvas canvas) { /* * create the arrays that will contain aliens */ shield1 = new Shield[ROWS][COLUMNS]; shield2 = new Shield[ROWS][COLUMNS]; shield3 = new Shield[ROWS][COLUMNS]; /* * create the shields */ for (int row = 0; row < shield1.length; row++) { for (int col = 0; col < shield1[0].length; col++) { shield1[row][col] = new Shield (FIRST_SHIELD_X + (col * Shield.SHIELD_WIDTH), SHIELD_Y + (row + Shield.SHIELD_WIDTH) * Shield.SHIELD_HEIGHT, canvas); shield2[row][col] = new Shield (SECOND_SHIELD_X + (col * Shield.SHIELD_WIDTH), SHIELD_Y + (row + Shield.SHIELD_WIDTH) * Shield.SHIELD_HEIGHT, canvas); shield3[row][col] = new Shield (THIRD_SHIELD_X + (col * Shield.SHIELD_WIDTH), SHIELD_Y + (row + Shield.SHIELD_WIDTH) * Shield.SHIELD_HEIGHT, canvas); } } start (); } /** * Determine if the any of the shield pieces have been hit by a projectile * * @param projectile the projectile fired from the invaders or laser base * @return true or false depending whether or not the projectile hit the shield pieces */ public synchronized boolean i**** (FilledRect projectile) { if ((projectile.getWidth() == Rocket.ROCKET_WIDTH) && (projectile.getHeight() == Rocket.ROCKET_HEIGHT)) { int shieldsBroken = 0; for (int row = 0; row < shield1.length; row++) { for (int col = 0; col < shield1[0].length; col++) { if ((shield1[row][col] != null) && shield1[row][col].i**** (projectile)) { shield1[row][col] = null; if (shield1[row + 1][col] != null) { shield1[row + 1][col] = null; } shieldsBroken++; } } for (int col = 0; col < shield1[0].length; col++) { if ((shield2[row][col] != null) && shield2[row][col].i**** (projectile)) { shield2[row][col] = null; if (shield2[row + 1][col] != null) { shield2[row + 1][col] = null; } shieldsBroken++; } } for (int col = 0; col < shield1[0].length; col++) { if ((shield3[row][col] != null) && shield3[row][col].i**** (projectile)) { shield3[row][col] = null; if (shield3[row + 1][col] != null) { shield3[row + 1][col] = null; } shieldsBroken++; } } } if (shieldsBroken > 0) { return true; } return false; } else if ((projectile.getWidth() == Missile.WIDTH) && (projectile.getHeight() == Missile.HEIGHT)) { for (int row = 0; row < shield1.length; row++) { for (int col = 0; col < shield1[0].length; col++) { if ((shield1[row][col] != null) && shield1[row][col].i**** (projectile)) { shield1[row][col] = null; if (shield1[row + 1][col] != null) { shield1[row + 1][col] = null; } return true; } else if ((shield2[row][col] != null) && shield2[row][col].i**** (projectile)) { shield2[row][col] = null; if (shield2[row + 1][col] != null) { shield2[row + 1][col] = null; } return true; } else if ((shield3[row][col] != null) && shield3[row][col].i**** (projectile)) { shield3[row][col] = null; if (shield3[row + 1][col] != null) { shield3[row + 1][col] = null; } return true; } } } return false; } else { for (int row = 0; row < shield1.length; row++) { for (int col = 0; col < shield1[0].length; col++) { if ((shield1[row][col] != null) && shield1[row][col].i**** (projectile)) { shield1[row][col] = null; return true; } else if ((shield2[row][col] != null) && shield2[row][col].i**** (projectile)) { shield2[row][col] = null; return true; } else if ((shield3[row][col] != null) && shield3[row][col].i**** (projectile)) { shield3[row][col] = null; return true; } } } return false; } } }
when a single shield is hit, (the .i****(FilledRect projectile) ) method, it just hides the projectile, as well as hides the shield piece which is the filledrect
thank you!!!
Comment