Web Analytics Made Easy -
StatCounter Help with my SpaceInvaders project please!! - CodingForum

Announcement

Collapse
No announcement yet.

Help with my SpaceInvaders project please!!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with my SpaceInvaders project please!!

    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:

    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!!!
    Last edited by Fou-Lu; Aug 30, 2011, 02:25 PM.

  • #2
    sorry, this is my first post so i dont really know how to use the [CODE] tags..

    Comment


    • #3
      the asterisks are supposed to show .is Hit (parameters)

      also, my issues are where it says, shield[row + 1] [col]

      is there another way to remove 2 shields at once?

      Comment


      • #4
        Originally posted by xobile2 View Post
        sorry, this is my first post so i dont really know how to use the [CODE] tags..
        No worries, its just like html so you need an [/code] tag. I'll chat with the Admin as well to see if we can drill that language filter a little bit to prevent that from converting to ****'s. Funny though.

        There's quite a bit of code here to go over, with a lot of it going in blind. I can judge fairly well what a lot of these datatypes are at a lower level, but not all of them.
        The logic seems a little strange to me, but without the full picture it does make it a little tough to evaluate. To me, I'd consider a Shield as a wrapper. So if you had three levels of sheid, and I had a Ship object, I would create the Shield class to compose of the Ship and wrap them over and over again. Each hit from a weapon will either reduce the shield (based on some sort of rating, which I think is the idea you're going for here), or remove it. So the idea would be that a Shield is a typeof Ship or more specifically it would be a common interface (IShieldable or something like that). That way you can provide a ship to a shield, and the result would still be of type ship. If that makes any sense.

        Now as for your actual NullPointerException, this is simply an indication of what you are trying to access is either not declared or initialized. In this case, it will not be declared. So checking the actual col of the uninitialized array is what is killing that. You'd need to check if the row is not null first, then check the column.

        An idea that could be rewritten with what you have is to take the 'remainder' of attack damage. So if 2x5 would remove 1 level of shield for example, you could remove the one level of shield, subtract the 2x5 or whatever from the matrix attack, and recurse the method until there is no more ammo to hit. That would let you remove multiple levels of the shield until either the artillery is consumed or the shield (ship maybe?) is destroyed.

        Hope that gives you some ideas to work with!

        Edit:
        My bad, its an out of bounds exception. That's ok, the solution I've posted was for the out of bounds exception and not the NullPointerException.
        Last edited by Fou-Lu; Aug 30, 2011, 02:53 PM.
        PHP Code:
        header('HTTP/1.1 420 Enhance Your Calm'); 
        Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

        Comment

        Working...
        X