Results 1 to 14 of 14
- 03-07-2015, 07:29 PM #1
Member
- Join Date
- Sep 2014
- Posts
- 21
- Rep Power
- 0
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; } } }
- 03-07-2015, 08:51 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-08-2015, 10:43 PM #3
Member
- Join Date
- Sep 2014
- Posts
- 21
- Rep Power
- 0
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.
- 03-08-2015, 10:50 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
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?
- 03-08-2015, 11:03 PM #5
Member
- Join Date
- Sep 2014
- Posts
- 21
- Rep Power
- 0
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.
- 03-08-2015, 11:29 PM #6
Member
- Join Date
- Sep 2014
- Posts
- 21
- Rep Power
- 0
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; } } } }
- 03-08-2015, 11:43 PM #7
Re: Need help with an array in Space Invaders program
Nothing happensIf you don't understand my response, don't ignore it, ask a question.
- 03-09-2015, 12:07 AM #8
Member
- Join Date
- Sep 2014
- Posts
- 21
- Rep Power
- 0
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; }
- 03-09-2015, 12:13 AM #9
Re: Need help with an array in Space Invaders program
nothing is happening when I run the program.
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.
- 03-09-2015, 12:17 AM #10
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-09-2015, 12:24 AM #11
Member
- Join Date
- Sep 2014
- Posts
- 21
- Rep Power
- 0
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!
- 03-09-2015, 12:50 AM #12
Member
- Join Date
- Sep 2014
- Posts
- 21
- Rep Power
- 0
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; }
Java Code:if (!aliens[row][col].isBlocked(aliens[row], aliens[col]))
- 03-09-2015, 12:58 AM #13
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-09-2015, 01:12 AM #14
Member
- Join Date
- Sep 2014
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
Space Warrior - Multiplayer Space Shooter
By stes in forum Reviews / AdvertisingReplies: 2Last Post: 01-11-2012, 09:19 AM -
Problem with Space Invaders Game
By andyman99008 in forum New To JavaReplies: 8Last Post: 12-19-2010, 10:20 PM -
Space Invaders help!
By Midge in forum New To JavaReplies: 5Last Post: 03-04-2010, 05:38 PM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 07:53 PM -
Get item from string array and pack with blank space
By firewalll in forum New To JavaReplies: 2Last Post: 09-02-2009, 08:38 AM
Bookmarks