Note that there are some explanatory texts on larger screens.

plurals
  1. POCollision detection with arrays/lists
    primarykey
    data
    text
    <p>I'm new to collision detection and having some trouble getting mine to work in my game prototype. Been at this for several days. After some reading and tutorials I'm still not understanding what I'm doing wrong so figure others here should be able to explain what I'm doing wrong. </p> <p>ATM the game runs and has 10 green rectangles (npcs) and 1 red rectangle (vampire) wonder the screen. As a jumping off point I want the vampire to "drink blood" when it randomly intersects a npc via dumb luck. If I don't comment out my collision detection method I get a nullpointexception in the code.</p> <p>public class Screen extends JPanel implements Runnable {</p> <pre><code>public Thread gameLoop = new Thread (this); public static int myWidth, myHeight; public static boolean isFirst = true; public static Npc npc; public static Vamp vamp; public static Npc[] npcs = new Npc[10]; //in future will have npcs and vamp count increase/decreasable public static Vamp[] vamps = new Vamp[1]; public Screen(Frame frame){ gameLoop.start(); } public void define(){ npc = new Npc(); vamp = new Vamp(); for(int i=0;i&lt;npcs.length;i++){ npcs[i] = new Npc(); } for(int i=0;i&lt;vamps.length;i++){ vamps[i] = new Vamp(); } } public void paintComponent(Graphics g){ if(isFirst){ myWidth = getWidth(); myHeight = getHeight(); define(); for(int i=0;i&lt;npcs.length;i++){ spawnVillagers(); } for(int i=0;i&lt;vamps.length;i++){ spawnVamp(); } isFirst = false; } g.setColor(new Color(192,192,192)); g.fillRect(0, 0, getWidth(), getHeight()); for(int i=0;i&lt;npcs.length;i++){ if(npcs[i].inGame){ npcs[i].draw(g); } } for(int i=0;i&lt;vamps.length;i++){ if(vamps[i].inGame){ vamps[i].draw(g); } } } public void spawnVillagers(){ for(int i=0;i&lt;npcs.length;i++){ if(!npcs[i].inGame){ npcs[i].spawn(); break; } } } public void spawnVamp(){ for(int i=0;i&lt;vamps.length;i++){ if(!vamps[i].inGame){ vamps[i].spawn(); break; } } } public void checkCollision(){ for(int i=0;i&lt;vamps.length;i++){ Vamp v = (Vamp) vamps[i]; Rectangle vampSpace = v.bounds(); //nullpoint error for(int n=0;n&lt;npcs.length;n++){ Npc c = (Npc) npcs[n]; Rectangle npcSpace = c.bounds(); //nullpoint error if(vampSpace.intersects(npcSpace)){ //error here as well since it's not getting bounds vamp.gainBlood(); } } } } @Override public void run() { while (true){ if(!isFirst) { npc.physic(); vamp.physic(); for(int i=0;i&lt;npcs.length;i++){ if(npcs[i].inGame){ npcs[i].physic(); } } for(int i=0;i&lt;vamps.length;i++){ if(vamps[i].inGame){ vamps[i].physic(); } } } checkCollision(); repaint(); try{ Thread.sleep(1); } catch (Exception e){} } } } </code></pre> <p>public class Npc extends Rectangle{</p> <p><em>snip</em></p> <pre><code>public Rectangle bounds(){ return (new Rectangle (x, y, npcSize, npcSize)); } </code></pre> <p><em>snip</em></p> <pre><code> } } </code></pre> <p>}</p> <p>public class Vamp extends Rectangle{</p> <p><em>snip</em></p> <pre><code>public Rectangle bounds(){ return (new Rectangle (x, y, npcSize, npcSize)); } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload