1 Replies - 177 Views - Last Post: 16 August 2012 - 04:37 PM Rate Topic: -----

#1 MDJboss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-August 12

Collision detection

Posted 16 August 2012 - 04:25 PM

I'm making a small java 2d game and i have followed a tutorial for collision detection. yet after implementing it into my code nothing happens when the rectangles Bound1 and PlayerR collide. I've tried following the tutorial again yet still nothing happens. No errors show up.

Thanks in Advance



import java.awt.Graphics;
002	import java.awt.Graphics2D;
003	import java.awt.Image;
004	import java.awt.Point;
005	import java.awt.PointerInfo;
006	import java.awt.Rectangle;
007	import java.awt.event.ActionEvent;
008	import java.awt.event.ActionListener;
009	import javax.swing.Icon;
010	import javax.swing.ImageIcon;
011	import javax.swing.JButton;
012	import javax.swing.JFrame;
013	import javax.swing.JPanel;
014	import javax.swing.Timer;
015	import javax.swing.plaf.basic.BasicSplitPaneUI.KeyboardHomeHandler;
016	import java.awt.MouseInfo;
017	import java.awt.event.FocusEvent;
018	import java.awt.event.FocusListener;
019	import java.awt.event.KeyEvent;
020	import java.awt.event.KeyListener;
021	import java.awt.event.MouseEvent;
022	import java.awt.event.MouseListener;
023	import java.awt.event.MouseMotionListener;
024	 
025	public class GamePanelLVL1 extends JPanel implements ActionListener, KeyListener{
026	ImageIcon BgI;
027	Image BgImage;
028	ImageIcon PlayerI;
029	Image PlayerImage;
030	double xvel, yvel;
031	int x = 50,y = 570;
032	Timer t = new Timer(5,this);
033	Rectangle PlayerR;
034	Rectangle Bound1;
035	    public GamePanelLVL1() {
036	t.start();
037	 PlayerR = new Rectangle(x,y,100,100);
038	 Bound1  = new Rectangle(100,100,100,100);
039	BgI = new ImageIcon(getClass().getResource("BgLVL1.png"));
040	BgImage  = BgI.getImage();
041	PlayerI = new ImageIcon(getClass().getResource("Player.png"));
042	PlayerImage  = PlayerI.getImage();
043	addKeyListener(this);
044	setFocusable(true);
045	setFocusTraversalKeysEnabled(false);
046	    }
047	public void paint(Graphics g){
048	    super.paint(g);
049	    Graphics2D Bg = (Graphics2D) g;
050	    Graphics2D Player = (Graphics2D) g;
051	    Graphics2D StringC = (Graphics2D) g;
052	    Bg.drawImage(BgImage, 0, 0, null);
053	    Player.drawImage(PlayerImage,x,y,100,100,null);
054	    Bg.fillRect(100, 100, 100, 100);
055	    Bg.fillRect(x, y, 100, 100);
056	}
057	 
058	    public void actionPerformed(ActionEvent e) {
059	        repaint();
060	        x +=xvel;
061	        y+=yvel;
062	        NoAcceletrate();
063	        if(PlayerR.intersects(Bound1))
064	            System.exit(0);
065	    }
066	 
067	public void NoAcceletrate(){
068	    if(xvel>1){
069	        xvel = 1;
070	    }
071	    if(xvel<-1){
072	        xvel = -1;
073	    }
074	    if(yvel<-1){
075	        yvel = -1;
076	    }
077	    if(yvel>1){
078	        yvel = 1;
079	    }
080	}
081	    public void keyPressed(KeyEvent e) {
082	        int key =e.getKeyCode();
083	        if(key==KeyEvent.VK_A){
084	            moveLeft();
085	        }
086	        if(key==KeyEvent.VK_D){
087	            moveRight();
088	        }
089	        if(key==KeyEvent.VK_SPACE){
090	            moveUp();
091	        }
092	         
093	    }
094	    private void moveLeft() {
095	        xvel-=1.5;
096	         
097	    }
098	    private void moveRight() {
099	        xvel+=1.5;
100	         
101	    }
102	    private void moveUp() {
103	        yvel-=1.5;
104	         
105	    }
106	    public void keyReleased(KeyEvent e) {
107	        xvel = 0;
108	        yvel = 0;
109	         
110	    }
111	    public void keyTyped(KeyEvent e) {
112	        // TODO Auto-generated method stub
113	         
114	    }
115	 
116	 
117	}



Is This A Good Question/Topic? 0
  • +

Replies To: Collision detection

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2111
  • View blog
  • Posts: 8,788
  • Joined: 20-September 08

Re: Collision detection

Posted 16 August 2012 - 04:37 PM

PlayerR's bounds get set once and never get updated. btw, Java variable names should be in camel case and begin lower case

This post has been edited by g00se: 16 August 2012 - 04:38 PM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1