Hi so I wanted to create 2 shields in my Space Invaders class that you can hit with a missile or bomb, but when I created them both, only one you are able to shoot at... here are my segments of code
Shield Class
public class Shield extends ActiveObject { /* * constants */ private static final int ROW = 25; private static final int COL = 25; /* * instance fields */ private FilledRect[][] shield; /* * class fields */ /** * Shield constructor * * This is the shield constructor that creates the shields for the game * * @param x the X coordiante where the shield is created * @param y the Y coordinate where the shield is created * @param canvas the drawing canvas of the game */ public Shield (double x, double y, DrawingCanvas canvas) { shield = new FilledRect[ROW][COL]; Random randGen = new Random (); for (int row = 0; row < shield.length; row++) { for (int col = 0; col < shield[0].length; col++) { shield[row][col] = new FilledRect (x + row * 3, y + col * 3, 3, 3, canvas); switch (randGen.nextInt (5)) { case 0: shield[row][col].setColor (Color.PINK); break; case 1: shield[row][col].setColor (Color.CYAN); break; case 2: shield[row][col].setColor (Color.YELLOW); break; case 3: shield[row][col].setColor (Color.ORANGE); break; case 4: shield[row][col].setColor (Color.MAGENTA); break; } } } } /** * isHit method * * This is the isHit method that determines whether or not the shield has been hit * and then creates a hole if it has been. * * @param missile the missile shot off by the laser base * @param bomb the bomb shot off by the aliens * @return true or false depending on whether or not the shield is hit by the missile or bomb */ public synchronized boolean isHit (FilledRect missile, FilledRect bomb) { for (int row = 0; row < shield.length; row++) { for (int col = 0; col < shield[0].length; col++) { if (!shield[row][col].isHidden () && shield[row][col].overlaps (missile)) { shield[row][col].hide (); missile.hide (); return true; } else if (!shield[row][col].isHidden () && shield[row][col].overlaps (bomb)) { shield[row][col].hide (); bomb.hide (); return true; } } } return false; } }
Space Invaders Class where I create the shields
Shield shield1 = new Shield (SpaceInvaders.BOTTOM_BORDER / 3 - 50, SpaceInvaders.RIGHT_BORDER / 3 + 50, canvas); base.setTarget (shield1); Shield shield2 = new Shield (SpaceInvaders.BOTTOM_BORDER / 3 + 150, SpaceInvaders.RIGHT_BORDER / 3 + 50, canvas); base.setTarget (shield2); /* * Set targets */ base.setTarget (ship); base.setTarget (invaders); invaders.setTarget (shield1); invaders.setTarget (shield2);
can anyone please help me why only one of the shields is working???? Its kind of urgent, since this is due in 2 days...