Results 1 to 7 of 7
  1. #1
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default 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

    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;
    
    
      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);
    	}
          }
        }
      }
    Thanks for any help with this. :D
    Last edited by Fortu; 12-20-2010 at 12:33 AM. Reason: Cut out all no-error sections of code

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Java Code:
     rect2.get(p) = rectalien2;
    rect2 was declared but never initalised... rect2.get(p) does not exist.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  4. #4
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    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

  5. #5
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    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 04:51 PM. Reason: Making error part more easily seen

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  7. #7
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default New code

    Well, I looked at my code, and I fixed the errors on my own, so now my collision detection works without any known problems :D

    Thanks for helping me in any way you guys could. You gave me the ideas for coding that I needed to do it right!

Similar Threads

  1. Collision
    By shadycharacter in forum New To Java
    Replies: 0
    Last Post: 04-13-2010, 10:58 PM
  2. Collision Detection
    By dotabyss in forum Java Gaming
    Replies: 0
    Last Post: 03-14-2010, 07:13 PM
  3. Replies: 3
    Last Post: 03-08-2009, 02:51 AM
  4. Collision Detection (Game)
    By mscwd in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 01-28-2008, 09:34 PM
  5. Two Problems Rotating and collision detection help
    By jaferris in forum Java Applets
    Replies: 2
    Last Post: 01-08-2008, 12:19 AM

Tags for this Thread

Posting Permissions

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