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.
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.
public class Screen extends JPanel implements Runnable {
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<npcs.length;i++){
npcs[i] = new Npc();
}
for(int i=0;i<vamps.length;i++){
vamps[i] = new Vamp();
}
}
public void paintComponent(Graphics g){
if(isFirst){
myWidth = getWidth();
myHeight = getHeight();
define();
for(int i=0;i<npcs.length;i++){
spawnVillagers();
}
for(int i=0;i<vamps.length;i++){
spawnVamp();
}
isFirst = false;
}
g.setColor(new Color(192,192,192));
g.fillRect(0, 0, getWidth(), getHeight());
for(int i=0;i<npcs.length;i++){
if(npcs[i].inGame){
npcs[i].draw(g);
}
}
for(int i=0;i<vamps.length;i++){
if(vamps[i].inGame){
vamps[i].draw(g);
}
}
}
public void spawnVillagers(){
for(int i=0;i<npcs.length;i++){
if(!npcs[i].inGame){
npcs[i].spawn();
break;
}
}
}
public void spawnVamp(){
for(int i=0;i<vamps.length;i++){
if(!vamps[i].inGame){
vamps[i].spawn();
break;
}
}
}
public void checkCollision(){
for(int i=0;i<vamps.length;i++){
Vamp v = (Vamp) vamps[i];
Rectangle vampSpace = v.bounds(); //nullpoint error
for(int n=0;n<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<npcs.length;i++){
if(npcs[i].inGame){
npcs[i].physic();
}
}
for(int i=0;i<vamps.length;i++){
if(vamps[i].inGame){
vamps[i].physic();
}
}
}
checkCollision();
repaint();
try{
Thread.sleep(1);
} catch (Exception e){}
}
}
}
public class Npc extends Rectangle{
snip
public Rectangle bounds(){
return (new Rectangle (x, y, npcSize, npcSize));
}
snip
}
}
}
public class Vamp extends Rectangle{
snip
public Rectangle bounds(){
return (new Rectangle (x, y, npcSize, npcSize));
}
}
NullPointerExceptionhas nothing to do with collision detection, and occurs when you try to deference an object which is not initialized, or is initialized tonull. I recommend that you step through your code with a debugger to see what's happening. - Matt Ball