Results 1 to 8 of 8
  1. #1
    heysayjump is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default Collision in GUI 2d Game help

    I have a problem with this code and we can't find where the error is. The method getBounds() or checkCollisions seem to have a problem. Can someone help us?
    checkCollision is supposed to the check whether the two objects are colliding and getBounds() is supposed to return the size of the rectangle image.

    Java Code:
    package mpGame1;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    
    import javax.swing.*;
    
    public class Board extends JPanel implements ActionListener, Runnable{
    Body p;
    Image img;
    Timer time; 	 
    Thread animator;
    static int v = 300;
    Enemy en;
    Enemy en2;
    
    boolean lost = false;
    
    	public Board(){
    		p = new Body();
    		addKeyListener(new AL());
    		setFocusable(true);
    		ImageIcon i = new ImageIcon("H:/College/OBJECTP/MP java game/stage1.png");
    		img  = i.getImage();
    		time = new Timer(5, this);
    		time.start();
            en = new Enemy((p.x + 550),300,"H:/College/OBJECTP/MP java game/real_mummy.png");
            en2 = new Enemy((p.x + 700),300,"H:/College/OBJECTP/MP java game/real_mummy.png");
    	
    	
    	}
    	
    	public void actionPerformed(ActionEvent e){
    	
    		checkCollisions();
    		//System.out.println(p.x);
    		ArrayList bullets = Body.getBullets();
    		for(int w = 0;w<bullets.size();w++){
    			
    			bullet m = (bullet)bullets.get(w);
    		    if(m.getVisible() == true){
    			m.move();	
    		    }
    		    else{
    		    	bullets.remove(w);
    		}
    		}
    		
    		
    		
    		
    		p.move();
    		
    		if(p.x > 400){
    			en.move(p.getDx(),p.getLeft());
    		}
    		if(p.x >  500){
    			en2.move(p.getDx(),p.getLeft());
    		}
    			
    		
    		repaint();
    		
    	}
    /**/
    	public void checkCollisions(){
    		
    		Rectangle r1 = en.getBounds();
    		Rectangle r2 = en2.getBounds();
    		ArrayList bullets = Body.getBullets();
    		for(int w = 0;w<bullets.size();w++){
    			
    			bullet m = (bullet) bullets.get(w);
    		    Rectangle m1 = m.getBounds();
    		    if(r1.intersects(m1) && en.Alive()){
    		    	en.isAlive = false;
    		    	m.visible = false;
    		    }
    		    else if(r2.intersects(m1) && en2.Alive())	{
    		    	en2.isAlive = false;
    		    	m.visible = false;		    	
    		    }
    		}
    		Rectangle d = p.getBounds();
    		if(d.intersects(r1) || d.intersects(r2)){	
    			lost = true;
    		}
    	}
    	
    	/**/
    boolean k = false;
    	
    	public void paint(Graphics g){
    		
    	if(lost){
    		System.exit(0);
    	}
    		if(p.dy == 1 && k ==false){
    			k = true;
    			animator = new Thread(this);
    			animator.start();
    		}
    		super.paint(g);
    			Graphics2D g2d = (Graphics2D) g;
    			
    			if((p.getX() - 90) % 1900 == 0){
    				p.nx = 0;
    			}
    			if((p.getX() - 1015) % 1900 == 0){
    				p.nx2 = 0;
    			}
    			g2d.drawImage(img, 1019-p.nx2, 0, null);
    			if(p.getX() >= 89){
    				g2d.drawImage(img, 1019-p.nx, 0, null);
    			}
    			g2d.drawImage(p.getImage(), p.getLeft(), v, null);
    	
    			
    			ArrayList bullets = Body.getBullets();
    			for(int w = 0;w<bullets.size();w++){
    		       		
    				bullet m = (bullet) bullets.get(w);
    				g2d.drawImage(m.getImage(),m.getX(),m.getY(),null);
    			    
    				if(p.x > 400){
    					if(en.Alive() == true){
    					   g2d.drawImage(en.getImage(),en.getX(),en.getY(),null);
    					   }
    					}
    				
    				if(p.x >  500){
    					if(en2.Alive() == true){
    						   g2d.drawImage(en2.getImage(),en2.getX(),en2.getY(),null);
    						   }
    					}
    			}
    	}
    	
    	private class AL extends KeyAdapter{
    		public void keyReleased(KeyEvent e){
    			p.keyReleased(e);
    		}
    		
    		public void keyPressed(KeyEvent e){
    			p.keyPressed(e);
    		}
    	}
    	
    	public void run(){
    		long beforeTime, timeDiff, sleep;
    		
    		beforeTime = System.currentTimeMillis();
    		while(done == false)
    		{
    		 	cycle();
    			timeDiff = System.currentTimeMillis() - beforeTime;
    			sleep = 10 - timeDiff;
    			if(sleep < 0){
    				sleep = 2;
    			}
    			try{
    				Thread.sleep(sleep);
    			}catch(Exception e)
    			{}
    			beforeTime = System.currentTimeMillis();
    		}
    		done = false;
    		h = false;
    		k = false;
    	}
    	boolean h = false;
    	boolean done = false;
    	public void cycle(){
    		if(h == false){
    			v = v -3;
    		}
    		if(v == 150){
    			h = true;
    		}
    		if(h == true && v <= 300){ 
    			v = v + 3;
    			if(v == 300){
    				done = true;
    			}
    		}
    	}
    	
    	public int getV(){	
    		return v;
    	}
    	
    	
    }
    Java Code:
    package mpGame1;
    
    import java.awt.*;
    
    import javax.swing.ImageIcon;
    
    public class Enemy {
    
    	Image img;
    	int x,y;
    	
    	boolean isAlive = true;
    	
    	public Enemy(int startX,int startY,String Location){
    		
    		x = startX;
    		y = startY;
    		ImageIcon l = new ImageIcon(Location);
    		img = l.getImage();
    		
    	}
    	
    	
    	public Rectangle getBounds(){
    		return new Rectangle(x,y,392,400);
    		
    	}
    
    	public int getX(){
    		return x;	
    	}
    	
    	public int getY(){
    		return y;	
    	}
    	
    	
    	
    	public boolean Alive(){
    		  return isAlive;
    		
    	}
    	
    	
    	
    	public Image getImage(){
    		
    		return img;
    	}
    
    	public void move(int dx,int left){
    	   if(dx == 1 && !((left + dx) < 150)){	
    		x = x - dx;
    	   }	   
    	}
    	
    
    	
    }

  2. #2
    heysayjump is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Bump bump bump bump bump bump bump bump bump bump bump bump

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Java Code:
    ArrayList bullets = Body.getBullets();
    Why are you accessing Body from a static context?

  4. #4
    heysayjump is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    oh so it is not allowed. Sorry I still can't fully understand all the concepts in java. How can I correct this part of the code?

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Its no problem.

    The conceptual difference between something that is static, and something that isn't is that static methods/values are known as Class Methods/Variables.

    The practical difference is that a static method/variable can be called simply by invoking the name of a class. For example: Math.pi or Math.sqrt(someNumber). The math class does not need to be instantiated before you use these things, they can be used any time.

    Non-static methods/variables can be thought of as instance methods/variables. They only exist after a class is instantiated - for example:
    Java Code:
    SomeClass derp = new SomeClass();
    derp.doSomething();
    Generally the only things that should be static are singleton objects (there will only ever be one of SomeClass), frequently used constants (Math.pi), or functions that are used frequently (Math.sqrt()).

    So, after reading all this, does it make sense to reference Body in the static context? 99% of the time, things should not be static. You deal mostly with objects who only have a meaning when instantiated.

  6. #6
    heysayjump is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Thanks. It makes sense but I still can't get my code to work when we add checkCollisions() and getBounds(). The program won't respond to the key events. Those methods are important so that the enemy will die if shot by the bullet.

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Well it all might be related - You declare a Body called 'p' at the top, but then reference it statically down below. Is that correct? Can I see the Body code?
    Last edited by quad64bit; 03-13-2011 at 09:41 PM. Reason: Typo

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Java Code:
    	public void checkCollisions(){
    		
    		Rectangle r1 = en.getBounds();
    		Rectangle r2 = en2.getBounds();
    		ArrayList bullets = Body.getBullets();
    		for(int w = 0;w<bullets.size();w++){
    			
    			bullet m = (bullet) bullets.get(w);
    		    Rectangle m1 = m.getBounds();
    		    if(r1.intersects(m1) && en.Alive()){
    		    	en.isAlive = false;
    		    	m.visible = false;
    		    }
    		    else if(r2.intersects(m1) && en2.Alive())	{
    		    	en2.isAlive = false;
    		    	m.visible = false;		    	
    		    }
    		}
    		Rectangle d = p.getBounds();
    		if(d.intersects(r1) || d.intersects(r2)){	
    			lost = true;
    		}
    	}
    I think there could be an issue with the IF-THEN-ELSEIF statement...
    It says:
    *If enemy1's bounds intersects with the bullets,
    *Then enemy1 = dead
    *Else if enemy2's bounds intersects with the bullets,
    *Then enemy2 = dead

    Which means that, if enemy1 is dead (or has its bounds intersected with bullet bounds), it will not check if enemy2 is dead or not, because the first branch is true so the if-then-else statement will break and not perform the else-if. If you take out the "else" it should sort that out with 2 separate IF-THEN statements.

    Also I would use a FOR-EACH loop rather than write how you've done.
    Java Code:
    for(bullet b:bullets) {
     Rectangle aBullet = b.getBounds();
     if (r1.intersects(aBullet) {
      en.isAlive = false;
      b.visible = false;
     }
     ...
    }
    Aside from that the logic seems fine but you've called the class variable for Enemy (en.isAlive) but you have not stated that variable to be Public; in your code in Enemy class it just says:

    Java Code:
    boolean isAlive = true;
    So either add 'public' to that line or you should make another public method in the Enemy class:

    Java Code:
    public void setAlive(boolean b) {
     isAlive = b;
    }
    And call to it instead, same how you've already called .Alive()
    Last edited by ozzyman; 03-14-2011 at 03:06 AM.

Similar Threads

  1. Need help with simple game! *collision*
    By CNew in forum New To Java
    Replies: 1
    Last Post: 12-05-2010, 01:09 AM
  2. Collision
    By shadycharacter in forum New To Java
    Replies: 0
    Last Post: 04-13-2010, 10:58 PM
  3. Collision Detection
    By dotabyss in forum Java Gaming
    Replies: 0
    Last Post: 03-14-2010, 07:13 PM
  4. Collision Detection (Game)
    By mscwd in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 01-28-2008, 09:34 PM
  5. Listener collision on game
    By cachi in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 08:48 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
  •