Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Collision Detection problem  (Read 3088 times)
0 Members and 1 Guest are viewing this topic.
Offline dotabyss

Junior Newbie





« Posted 2010-03-14 18:58:21 »

Here's the main code I have for my java space invaders clone:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
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)
         {
           
         }
      }
   }
}


Here's the problem:

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.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2010-03-14 19:33:23 »

Text formatting doesn't show in code brackets. I pasted your code into a text editor and line 150 is:

Rectangle rI = inimigos[a].getLimites();

It's a NullPointerException, which means that you're referencing a variable that hasn't been instantiated with new. The only variable you're referencing is inimigos[a], so that's what's null.

I'm guessing that run() is getting called before inicializarInimigos(), although run() never appears to be called anywhere in your code. Either way, this is just a simple matter of tracing through to find out what's getting called when. If you want this code path order, you can always put in a:

if (inimigos[a] != null)

Which you appear to already have done elsewhere in your code. Actually because you're calling

inimigos[a] = null

every time a collision occurs, I'm sure that's your problem. You're calling that, which sets a value to null, and then trying to reference it again next timestep when you call checarColisao() again. Why not use an ArrayList instead of an array so that you don't leave null "holes" laying around? Otherwise I guess just add in a null check like I mentioned above.

See my work:
OTC Software
Offline dotabyss

Junior Newbie





« Reply #2 - Posted 2010-03-15 02:55:47 »

Thanks a lot!

The reason why I don't use ArrayList is because I want to make the game as simple as possible, but still functional (and I don't have time to learn how to use it properly).

The new code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
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++;
                  }
               }
            }
         }
      }
   }

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);
      }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #3 - Posted 2010-03-15 21:16:47 »

Now that's just silly. You've already wasted all the time it would take to learn ArrayList by posting your null pointer question on JGO.

Array:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
//Creation.
int[] arr = new int[50];

//Assignment.
arr[0] = 124;

//Value reference.
int i = arr[12];

//Getting length.
int l = arr.length;

//Iterating through.
for (int i = 0; i < arr.length; i++)
    System.out.println(arr[i]);


ArrayList:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
//Creation.
ArrayList<Integer> arr = new ArrayList<Integer>();

//Assignment (add at the end).
arr.add(124);

//Assignment (overwrite a value at an index).
arr.set(0, 124);

//Value reference.
int i = arr.get(12);

//Getting length.
int l = arr.size();

//Iterating through.
for (int i = 0; i < arr.size(); i++)
    System.out.println(arr.get(i));


For further reference,
http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (6 views)
2014-07-29 10:59:34

E.R. Fleming (20 views)
2014-07-29 03:07:13

E.R. Fleming (8 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!