0
votes

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));
}

}

1
NullPointerException has nothing to do with collision detection, and occurs when you try to deference an object which is not initialized, or is initialized to null. I recommend that you step through your code with a debugger to see what's happening. - Matt Ball
Are you able to isolate only the code necessary to demonstrate the problem? - Patashu
I snipped down the npc and vamp class to help the eyes. Didn't want to cut down Screen to much, like Matt said I'm not initializing/null something and I'm worried I might take out something that maybe key to my issue. (Still not very good with nullpointerexceptions or debuggers as this is my second program to write) - maebe

1 Answers

0
votes

TY Matt. I did some extra research on nullpointerException errors. This with looking over my code with fresh eyes fixed my issue. I was putting my checkcollision() to early in my code.