Results 1 to 9 of 9
  1. #1
    CNew is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default Why is my collision not working?

    Here Is My code. Everything is working except the collision of the 2 rectangles. I am not sure why it is not working. I highlighted the parts that have to do with it. Thanks for the help!


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.Rectangle;

    public class finalp extends JFrame implements KeyListener, ActionListener{
    public static final Component frame = null;
    ImageIcon ball;
    int x1;
    int y1;
    int x;
    int y;
    ImageIcon hurt;
    Timer clock;
    Rectangle r = new Rectangle(200,100,20,30);
    Rectangle r2 = new Rectangle(800,550,20,30);
    boolean collide, collide2;

    public static void main(String args[]){
    new finalp();


    } // end main
    public finalp(){
    super("Final");
    clock = new Timer(50, this);

    this.setSize(1000, 1000);
    this.setVisible(true);
    this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    this.addKeyListener(this);
    clock.start();



    } // end constructor
    public boolean intersects;{
    collide = r.intersects (r2);
    collide2 = r2.intersects(r);
    if (collide)
    JOptionPane.showMessageDialog(frame, "You Lose");
    if(collide2)
    JOptionPane.showMessageDialog(frame, "You Lose");

    }
    public void paint(Graphics g) {
    super.paint(g);



    g.setColor(Color.GREEN);
    g.fillRect(r2.x,r2.y,r2.width,r2.height);

    g.setColor(Color.WHITE);
    g.fillRect(x, y, r.width, r.height);



    } // end paint








    public void keyTyped(KeyEvent e){
    }

    public void keyPressed(KeyEvent e){

    int theKey = e.getKeyCode();
    System.out.println(theKey);
    if (theKey == KeyEvent.VK_LEFT){
    x -= 20;
    } else if (theKey == KeyEvent.VK_RIGHT){
    x += 20;
    } else if (theKey == KeyEvent.VK_UP){
    y -= 20;
    } else if (theKey == KeyEvent.VK_DOWN){
    y += 20; // end if
    } else if (theKey == KeyEvent.VK_DOWN){
    y += 20; // end if



    } repaint();

    }

    public void keyReleased(KeyEvent e){

    }


    public void actionPerformed(ActionEvent e) {
    r2.y +=5;
    if (r2.y > getHeight()){
    r2.y = 0;


    }
    repaint();
    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You might want to edit your code so that it's readable. Put [CODE] at the start and [/CODE] at the end. This will allow the indenting to be preserved.

    I am not sure why it is not working

    What does "not working" mean? Describe what happens when you run the code and what you expected or intended would happen.

    Note that you don't actually call the intersects() method anywhere.

  3. #3
    CNew is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    i control the white rectangle with the arrow keys, and the green rectangle is moving on its own. That is working fine. But, i want a message to pop up and say "you lose" when the two rectangles intersect. Nothing is happening when they intersect. And i didnt know about the
    Java Code:
    ,
    ! sorry!

  4. #4
    CNew is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.Rectangle;
    
    public class finalp extends JFrame implements KeyListener, ActionListener{
      public static final Component frame = null;
    ImageIcon ball;
      int x1;
      int y1;
      int x;
      int y;
      ImageIcon hurt;
      Timer clock;
      Rectangle r = new Rectangle(200,100,20,30);
      Rectangle r2 = new Rectangle(800,550,20,30);
      boolean collide, collide2;
      
      public static void main(String args[]){
    	  new finalp();
    	 
    	  
      } // end main
       public finalp(){
    	  super("Final");
    	  clock = new Timer(50, this);
    
    	  this.setSize(1000, 1000);
    	  this.setVisible(true);
    	  this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    	   this.addKeyListener(this);	  
              clock.start();
              
              
              
              }  // end constructor
      [COLOR="Red"]public boolean intersects;{
    	  collide = r.intersects (r2);
    	  collide2 = r2.intersects(r);
    	    if (collide)
    			JOptionPane.showMessageDialog(frame, "You Lose");
    		if(collide2)
    			JOptionPane.showMessageDialog(frame, "You Lose"); [/COLOR]
      
      }
      public void paint(Graphics g) {
    	  super.paint(g);
    	  
    	  	
    	  
    		g.setColor(Color.GREEN);
    		g.fillRect(r2.x,r2.y,r2.width,r2.height);
    
    		g.setColor(Color.WHITE);
    		g.fillRect(x, y, r.width, r.height);
    	  
    	   
    	 
      } // end paint
      
      
    		
    	
      
    	  
    	
    	
      public void keyTyped(KeyEvent e){
    	} 
    
      public void keyPressed(KeyEvent e){
            
            int theKey = e.getKeyCode();
            System.out.println(theKey);
    	if (theKey == KeyEvent.VK_LEFT){
    	    x -= 20;
            } else if (theKey == KeyEvent.VK_RIGHT){
                x += 20;
            } else if (theKey == KeyEvent.VK_UP){
                y -= 20;
            } else if (theKey == KeyEvent.VK_DOWN){
                y += 20;   // end if
            }    else if (theKey == KeyEvent.VK_DOWN){
                    y += 20;   // end if 
                    
               
                
            } repaint();
            
      }
    
      public void keyReleased(KeyEvent e){
    	
        } 
     
    	  
    public void actionPerformed(ActionEvent e) {
    	r2.y +=5;
    	 if (r2.y > getHeight()){
    		   r2.y = 0;
    	
    	 
    	}
    	 repaint();
    }
    }

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Thanks for reposting the code.

    Did you understand about calling intersects()? You have to do this whenever the rectangle positions change.

  6. #6
    CNew is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    No, i am not sure what you mean. I am fairly new to this.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

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

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Thanks, locking the duplicate.

    OP, why are you double posting and thus unnecessarily splitting the discussion? Besides being unfair to forum members, it's against forum policy that you agreed to on joining here.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    i am not sure what you mean

    You have a method, intersects(), that checks for an intersection and reports it. You can think of a method like a small program. The point is that the method only "runs" when you call or invoke it. In other words that intersects() method won't actually do anything until we see a line like

    Java Code:
    intersects();

    somewhere (perhaps more than one place) within your code.

Similar Threads

  1. Collision between 2 rectangles
    By CNew in forum New To Java
    Replies: 1
    Last Post: 12-05-2010, 04:18 AM
  2. Need help with simple game! *collision*
    By CNew in forum New To Java
    Replies: 1
    Last Post: 12-05-2010, 12:09 AM
  3. Collision
    By shadycharacter in forum New To Java
    Replies: 0
    Last Post: 04-13-2010, 09:58 PM
  4. Collision Detection
    By dotabyss in forum Java Gaming
    Replies: 0
    Last Post: 03-14-2010, 06:13 PM
  5. Collision Detection (Game)
    By mscwd in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 01-28-2008, 08:34 PM

Posting Permissions

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