Results 1 to 1 of 1
Thread: Collision Detection
- 03-14-2010, 06:13 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 4
- Rep Power
- 0
Collision Detection
Here I am, again, with another thread problem...
Here's the main code I have for my java space invaders clone:
Here's the problem:Java Code:import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.*; public class Game extends JPanel implements Runnable, KeyListener{ public static void main (String args[]) { Game game = new Game(); game.construirJanela(); game.inicializarInimigos(); } public void construirJanela() { JFrame janela = new JFrame ("Game"); setBackground(Color.black); setFocusable(true); setDoubleBuffered(true); addKeyListener(this); janela.setSize(800, 600); janela.setResizable(false); janela.setLocationRelativeTo(null); janela.add(this); janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); janela.setVisible(true); } private Thread animador; private final int VELOCIDADE = 5; private final int ESPACO = 50; private int velInimigo; private Jogador jogador; private Inimigo[] inimigos; private Tiro[] tiros; private boolean inGame; public Game() { jogador = new Jogador(385, 500); tiros = new Tiro[5]; inimigos = new Inimigo[12]; inGame = true; } public void inicializarInimigos() { for (int i = 0; i < inimigos.length; i++) { inimigos[i] = new Inimigo(ESPACO * i, 20); } } public void addNotify() { super.addNotify(); animador = new Thread(this); animador.start(); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; if (inGame == true) { if (jogador.getJVisivel() == true) { jogador.desenharJogador(g2d); } for(int t = 0; t< tiros.length; t++) { if(tiros[t] != null) { tiros[t].desenharTiro(g2d); } } for (int i = 0; i < inimigos.length; i++) { if (inimigos[i].getIVisivel() == true) { inimigos[i].desenharInimigo(g2d); } } } else { Font fonte = new Font("Tahoma", Font.BOLD, 35); g2d.setFont(fonte); g2d.setColor(Color.white); g2d.drawString("Game Over", 320, 300); } } public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { jogador.moverJogadorX(-VELOCIDADE); } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { jogador.moverJogadorX(VELOCIDADE); } if (e.getKeyCode() == KeyEvent.VK_SPACE) { for(int i = 0; i < tiros.length; i++) { if(tiros[i] == null) { tiros[i] = jogador.atirar(); break; } } } if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { inGame = false; } } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } public void checarColisao() { Rectangle rJ = jogador.getLimites(); for (int a = 0; a < inimigos.length; a++) { [COLOR="Red"] Rectangle rI = inimigos[a].getLimites(); [/COLOR] if (rJ.intersects(rI)) { jogador = null; inimigos[a] = null; inGame = false; } } for (int b = 0; b < tiros.length; b++) { Rectangle rT= tiros[b].getLimites(); for (int c = 0; c < inimigos.length; c++) { Rectangle rI = inimigos[c].getLimites(); if (rT.intersects(rI)) { tiros[b] = null; inimigos[c] = null; } } } } public void run() { while (true) { for(int d = 0; d < tiros.length; d++) { if(tiros[d] != null) { tiros[d].moverTiro(-VELOCIDADE); if(tiros[d].getTiroY() < 0) { tiros[d] = null; } } } for (int e = 0; e < inimigos.length; e++) { if (inimigos[e] != null) { inimigos[e].moverInimigo(velInimigo); if (inimigos[e].getInimigoX() + 20 == 800) { velInimigo = - VELOCIDADE; } if (inimigos[e].getInimigoX() == 0) { velInimigo = VELOCIDADE; } } } [COLOR="red"] checarColisao(); [/COLOR] repaint(); try { Thread.sleep(25); } catch (InterruptedException e) { } } } }
Exception in thread "Thread-3" java.lang.NullPointerException
at Game.checarColisao(Game.java:150)
at Game.run(Game.java:211)
at java.lang.Thread.run(Thread.java:619)
"checarColisao()" is a method that retrieves a Rectangle from every entity in the game and then checks if they intersect each other. When they do, the method should get rid of the entity.
There's also a bug at the enemy's movement that I'll try to fix later, never mind.
I would be glad if someone could help me here. Thanks in advance.
EDITED:
Never mind, problem solved!
This code works:
Java Code:public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; if (inGame == true) { if (jogador.getJVisivel() == true) { jogador.desenharJogador(g2d); } for(int t = 0; t< tiros.length; t++) { if(tiros[t] != null) { tiros[t].desenharTiro(g2d); } } for (int i = 0; i < inimigos.length; i++) { if (inimigos [i] != null) { inimigos[i].desenharInimigo(g2d); } } g2d.setColor(Color.white); g2d.drawString("Inimigos restantes: " + (12 - inimigosMortos), 10, 10); } public void checarColisao() { Rectangle rJ = jogador.getLimites(); for (int a = 0; a < inimigos.length; a++) { if (inimigos[a] != null) { Rectangle rI = inimigos[a].getLimites(); if (rJ.intersects(rI)) { jogador = null; inimigos[a] = null; inGame = false; } } } for (int b = 0; b < tiros.length; b++) { if (tiros[b] != null) { Rectangle rT = tiros[b].getLimites(); for (int c = 0; c < inimigos.length; c++) { if (inimigos[c] != null) { Rectangle rI = inimigos[c].getLimites(); if (rT.intersects(rI)) { tiros[b] = null; inimigos[c] = null; inimigosMortos++; } } } } } }Last edited by dotabyss; 03-15-2010 at 02:26 AM. Reason: Problem solved
Similar Threads
-
USB Detection
By alanixu in forum New To JavaReplies: 3Last Post: 11-12-2008, 04:04 PM -
HashMap: Obtaining all values in a collision?
By markus-sukram in forum New To JavaReplies: 2Last Post: 03-29-2008, 10:25 PM -
Collision Detection (Game)
By mscwd in forum Sun Java Wireless ToolkitReplies: 0Last Post: 01-28-2008, 08:34 PM -
Two Problems Rotating and collision detection help
By jaferris in forum Java AppletsReplies: 2Last Post: 01-07-2008, 11:19 PM -
Listener collision on game
By cachi in forum Java AppletsReplies: 1Last Post: 08-07-2007, 07:48 AM


LinkBack URL
About LinkBacks

Bookmarks