Results 1 to 14 of 14
  1. #1
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Need help with an array in Space Invaders program

    Okay so I have this code.

    Java Code:
    	
    	public synchronized boolean isHit (FilledRect missile)
    	{
    		for (int col = 0; col < aliens[0].length; col++)
    		{
    			for (int row = 0; row < aliens.length; row++)
    			{
    				if (aliens[row][col]!= null && aliens[row][col].overlaps (missile))
    				{
    					aliens[row][col] = null;
    
    					return true;
    				}
    				return false;
    			}
    		}
    	}
    And I get an error saying that it cannot find symbol method overlaps. So is there something wrong that I am doing?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Need help with an array in Space Invaders program

    Your aliens array should be holding an object of some type. That type must have a method called overlaps that takes a FilledRect object as an argument. Apparently, that is not the case (or the method is not publicly accessible).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: Need help with an array in Space Invaders program

    Okay, I am doing this part incorrectly. I basically copied this piece of code from another class that was working with a visible image but for this class I need it to work with the array. The object the aliens are holding is of type Alien. Here is the direction for this step my teacher gave me.
    Create a synchronized method isHit() to determine if any of the living aliens have been hit by the missile and if hit, set the alien to null.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Need help with an array in Space Invaders program

    As Jim says there must be an accessible method overlaps() in the class of whatever the aliens array is supposed to contain.

    There's not enough context in what you posted to see what is causing the problem. Where is the aliens array declared? And what is the definition of the class of thing it is supposed to contain?

  5. #5
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: Need help with an array in Space Invaders program

    im using objectDraw.

    objectdraw.ActiveObject;
    objectdraw.DrawingCanvas;
    objectdraw.VisibleImage;
    objectdraw.FilledRect;

    The array is declared within the Invaders class. The Invaders class also contains the synchronized method I need to write. The Alien class contains all the information relevant for the alien including the actions the alien will perform and the images associated with the alien.

  6. #6
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: Need help with an array in Space Invaders program

    Also, I don't really understand synchronized methods. I had this method just to check something out.

    Java Code:
            public synchronized boolean isHit ()
    	{
    		for (int row = 0; row < aliens.length; row++)
    		{
    			for (int col = 0; col < aliens[0].length; col++)
    			{
    				if (aliens[row][col] != null)
    				{
    					System.out.println("hi");
    					aliens[row][col] = null;
    
    					return true;
    				}
    			}
    		}
            }
    Wouldn't all the aliens become null as soon as I run the program? Shouldn't I get the message on the console? Nothing happens though?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need help with an array in Space Invaders program

    Nothing happens
    Try debugging the code by adding some println() statements to show that the method is called and that each of the for loops executes.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: Need help with an array in Space Invaders program

    Okay now I have this code

    Java Code:
    	
            public synchronized boolean isHit (FilledRect missile)
    	{
    		for (int row = 0; row < aliens.length; row++)
    		{
    			for (int col = 0; col < aliens[0].length; col++)
    			{
    				if (aliens[row][col].isHit(theMissile))
    				{
    					System.out.println("hi");
    					aliens[row][col] = null;
    
    					return true;
    				}
    			}
    		}
    
    		return false;
    	}
    It compiles, however, nothing is happening when I run the program.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need help with an array in Space Invaders program

    nothing is happening when I run the program.
    I don't see any println() statements for debugging.

    How do you know if any code is executing? Add lots of println() statements to show what code is being executed.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Need help with an array in Space Invaders program

    Your last code snippet looks like it might be doing recursive calls. Are you familiar with recursion and its subtleties?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: Need help with an array in Space Invaders program

    THANK YOU!! I figured out the problem using a whole bunch of println() statements and found out I had to add an if statement to make sure the missile stops moving when it overlaps the alien!

  12. #12
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: Need help with an array in Space Invaders program

    Okay, now another problem. I have this boolean method.

    Java Code:
    	/**
    	 * Determine if the alien has other aliens below it
    	 *
    	 * @param row the row of the current alien
    	 * @param col the column of the current alien
    	 * @return true or false depending whether or not the alien is blocked
    	 */
    
    	public boolean isBlocked (int row, int col)
    	{
    		if (aliens[row - 1][col] == null)
    		{
    			return true;
    		}
    		return false;
    	}
    I wrote this method to make sure the alien can drop the bomb only if there is no alien below it. As you can see, I need the column number and row number, however, im not sure how to get them when I use the method. I wrote this to use isBlocked()

    Java Code:
    if (!aliens[row][col].isBlocked(aliens[row], aliens[col]))

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Need help with an array in Space Invaders program

    Just look at what your method takes as argument and look what you are actually passing. Just think about it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: Need help with an array in Space Invaders program

    Okay thanks I found it

Similar Threads

  1. Space Warrior - Multiplayer Space Shooter
    By stes in forum Reviews / Advertising
    Replies: 2
    Last Post: 01-11-2012, 08:19 AM
  2. Problem with Space Invaders Game
    By andyman99008 in forum New To Java
    Replies: 8
    Last Post: 12-19-2010, 09:20 PM
  3. Space Invaders help!
    By Midge in forum New To Java
    Replies: 5
    Last Post: 03-04-2010, 04:38 PM
  4. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  5. Get item from string array and pack with blank space
    By firewalll in forum New To Java
    Replies: 2
    Last Post: 09-02-2009, 07:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •