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

    Default Collision not being detected

    I am working on simply getting a message to come up that says "you lose" when my 2 rectangles touch each other. everything s working fine except that part!



    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();
    }
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Uff..

    a) public boolean intersects;{ <-- remove the semikolon + add brackets :)
    --> public boolean intersects(){

    b) then you have to return a boolean, or change it to void !

    c) you never call this method or? ;/

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

    Default

    i changed it to brackets, but i'm not sure what to return. i'm not too familiar with this. And what did u mean by your last comment?


    Java Code:
    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");
    		return [COLOR="Red"]__?___[/COLOR];

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

Similar Threads

  1. A fatal error has been detected by the Java Runtime Environment:
    By jayanti kumari in forum Advanced Java
    Replies: 8
    Last Post: 08-16-2010, 07:31 AM
  2. Collision
    By shadycharacter in forum New To Java
    Replies: 0
    Last Post: 04-13-2010, 09:58 PM
  3. show detected access point
    By motress in forum New To Java
    Replies: 1
    Last Post: 12-31-2009, 01:23 AM
  4. Replies: 3
    Last Post: 12-30-2008, 10:58 AM
  5. Replies: 0
    Last Post: 07-18-2008, 11:37 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
  •