Results 1 to 7 of 7
- 12-19-2010, 05:25 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 57
- Rep Power
- 0
Three errors in collision detection methods
SOLVED
I've been trying to make a game recently, and I've ran into some problems. I'm doing the collision detection right now (or at least trying to) and I've hit some problems I can't manage to solve (due to my lack of java knowledge).
I'm getting two unexpected type errors and one cannot find symbol error
Here's part of my code (Oh yes, sorry for all the terribly hard-to-keep-track-of variable names):
I took out all parts that had no errors, so focus can be put into the parts that don't run
Thanks for any help with this. :DJava Code:import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.ArrayList; public class InnerMaterial extends JPanel implements ActionListener { public Playuh playuh; public Timer timer; public Alien alien; public ArrayList first; public ArrayList second; public Rectangle rect1; public ArrayList<Rectangle> rect2 = new ArrayList<Rectangle>(); public ArrayList<Rectangle> rect3 = new ArrayList<Rectangle>(); public ArrayList ms; public ArrayList furst; public int p; public int w; public void updateRectangles() { rect1 = playuh.getBounds(); for(p=0;p<second.size();p++) { Rectangle rectalien2 = (Rectangle) rect2.get(p); AlienMonstuh rectalien3 = (AlienMonstuh) second.get(p); rectalien2 = rectalien3.getBounds(); rect2.get(p) = rectalien2; // 1 error here } for(int q=0;q<ms.size();q++) { (Rectangle)rect3.get(p) = (Shot)ms.get(p).getBounds(); // 2 errors here } } public void checkCollisions() { for(w=0;w<rect2.size();w++) { Rectangle rectalien = rect2.get(w); if(rectalien.intersects(rect1)) { System.exit(0); } } for(int e=0;e<rect3.size();e++) { for(int r=0;r<rect2.size();r++) { if(rect2.get(r).intersects(rect3.get(e))) { rect2.remove(r); rect3.remove(e); ms.remove(e); second.remove(r); furst.remove(e); first.remove(r); } } } }Last edited by Fortu; 12-19-2010 at 11:33 PM. Reason: Cut out all no-error sections of code
- 12-19-2010, 05:31 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
rect2 was declared but never initalised... rect2.get(p) does not exist.Java Code:rect2.get(p) = rectalien2;
-
You're calling get on the left hand side of an assignment:
Java Code:rect2.get(p) = rectalien2;
And this doesn't make much sense. Other than that, I find it hard to help you as your code won't compile for me given its many dependencies. I suggest you simplify the code and the problem to help you isolate the error.
- 12-19-2010, 08:23 AM #4
Member
- Join Date
- Dec 2010
- Posts
- 57
- Rep Power
- 0
I tried initialising rect2 and rect3, but no changes to the errors. Also, what I'm doing is updating each rectangle of a specified enemy (from rect2) and updating the rectangle of a specified shot (from rect3), and then checking for collisions in the checkCollisions method. I don't know how to store rectangle data back into the Arra-wait there's a method of ArrayList that allows the adding of an element at a specified integer. I could just remove whatever was in the spot on the arraylist and then use that method to store the updated rectangle in the same spot
- 12-19-2010, 03:46 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 57
- Rep Power
- 0
My idea didn't work.. And what I'm trying to do here is store a piece of specific rectangle data in the specified spot on the ArrayList.
My code now compiles but I'm getting a IndexOutOfBoundsException error now whenever I run the code, and I'm not sure what's wrong o_o
Scroll partway through the code to find the error-ridden section (I marked it)
Here's what I have now:
Java Code:import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.ArrayList; public class InnerMaterial extends JPanel implements ActionListener { public Playuh playuh; public Timer timer; public Alien alien; public ArrayList first; public ArrayList second; public Rectangle rect1; public ArrayList<Rectangle> rect2 = new ArrayList<Rectangle>(); public ArrayList<Rectangle> rect3 = new ArrayList<Rectangle>(); public ArrayList ms; public ArrayList furst; public int p; public int w; InnerMaterial() { super(); setBackground(Color.BLACK); setFocusable(true); addKeyListener(new TAdapter()); playuh = new Playuh(); setDoubleBuffered(true); setSize(600,465); alien = new Alien(); first = new ArrayList(); second = new ArrayList(); timer = new Timer(10, this); timer.start(); } public void paint (Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; g2d.drawImage(playuh.getImage(),playuh.getX(),playuh.getY(),this); furst = playuh.getShot(); for(int i=0;i<furst.size();i++) { Shot m = (Shot)furst.get(i); g2d.drawImage(m.getImage(), m.getX(), m.getY(), this); } first = alien.getAlienMonstuh(); for(int i=0;i<first.size(); i++) { AlienMonstuh enemy = (AlienMonstuh) first.get(i); g2d.drawImage(enemy.getImage(), enemy.getX(), enemy.getY(), this); } Toolkit.getDefaultToolkit().sync(); g.dispose(); } // START OF ERROR-RIDDEN CODE. // PLEASE BEGIN SEARCH FOR PROBLEMS HERE [B] public void updateRectangles() { rect1 = playuh.getBounds(); for(p=0;p<second.size();p++) { AlienMonstuh alienmonstuh1 = (AlienMonstuh) second.get(p); rect2.set(p,alienmonstuh1.getBounds()); } for(int q=0;q<ms.size();q++) { Shot shot1 = (Shot) ms.get(q); rect3.set(p,shot1.getBounds()); } } public void checkCollisions() { for(w=0;w<rect2.size();w++) { Rectangle rectalien = rect2.get(w); if(rectalien.intersects(rect1)) { System.exit(0); } } for(int e=0;e<rect3.size();e++) { for(int r=0;r<rect2.size();r++) { if(rect2.get(r).intersects(rect3.get(e))) { rect2.remove(r); rect3.remove(e); ms.remove(e); second.remove(r); furst.remove(e); first.remove(r); } } } }[/B] // END OF ERROR-RIDDEN CODE. // LOOK PAST IF YOU WANT TO, BUT THERE // SHOULDN'T BE ANY ERRORS.. public void actionPerformed(ActionEvent e) { ms = playuh.getShot(); for (int i = 0; i < ms.size(); i++) { Shot m = (Shot) ms.get(i); if(m.isVisible()) m.move(); else ms.remove(i); } second = alien.getAlienMonstuh(); for(int o=0; o<second.size(); o++) { AlienMonstuh enemy2 = (AlienMonstuh) second.get(o); enemy2.move(); } alien.addAlienMonstuh(); playuh.move(); updateRectangles(); checkCollisions(); repaint(); } private class TAdapter extends KeyAdapter { public void keyReleased(KeyEvent e) { playuh.keyReleased(e); } public void keyPressed(KeyEvent e) { playuh.keyPressed(e); } } }Last edited by Fortu; 12-19-2010 at 03:51 PM. Reason: Making error part more easily seen
-
Your above code has dependencies that we have no access to, such as Playuh, Alien, AlienMonstuh, Shot, etc, making it utterly impossible for me and others to compile, run, test, and modify your code. Please have a look at the 3rd link in my signature links below on how to create an SSCCE. If you can create and post a decent SSCCE, I'm almost positive that we'll be much better able to help you.
Best of luck!
- 12-19-2010, 11:32 PM #7
Member
- Join Date
- Dec 2010
- Posts
- 57
- Rep Power
- 0
Similar Threads
-
Collision
By shadycharacter in forum New To JavaReplies: 0Last Post: 04-13-2010, 09:58 PM -
Collision Detection
By dotabyss in forum Java GamingReplies: 0Last Post: 03-14-2010, 06:13 PM -
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03-08-2009, 01:51 AM -
Collision Detection (Game)
By mscwd in forum Sun Java Wireless ToolkitReplies: 0Last Post: 01-28-2008, 08:34 PM -
Two Problems Rotating and collision detection help
By jaferris in forum Java AppletsReplies: 2Last Post: 01-07-2008, 11:19 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks