Results 1 to 8 of 8
Thread: Collision in GUI 2d Game help
- 03-11-2011, 03:23 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
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; } } }
- 03-11-2011, 04:05 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
Bump bump bump bump bump bump bump bump bump bump bump bump
- 03-12-2011, 02:02 PM #3Java Code:
ArrayList bullets = Body.getBullets();
- 03-12-2011, 05:39 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
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?
- 03-12-2011, 10:29 PM #5
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();
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.
- 03-13-2011, 04:03 AM #6
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
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.
- 03-13-2011, 09:41 PM #7
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
- 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; } }
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; } ... }
Java Code:boolean isAlive = true;
Java Code:public void setAlive(boolean b) { isAlive = b; }
Last edited by ozzyman; 03-14-2011 at 03:06 AM.
Similar Threads
-
Need help with simple game! *collision*
By CNew in forum New To JavaReplies: 1Last Post: 12-05-2010, 01:09 AM -
Collision
By shadycharacter in forum New To JavaReplies: 0Last Post: 04-13-2010, 10:58 PM -
Collision Detection
By dotabyss in forum Java GamingReplies: 0Last Post: 03-14-2010, 07:13 PM -
Collision Detection (Game)
By mscwd in forum Sun Java Wireless ToolkitReplies: 0Last Post: 01-28-2008, 09:34 PM -
Listener collision on game
By cachi in forum Java AppletsReplies: 1Last Post: 08-07-2007, 08:48 AM
Bookmarks