【问题标题】:Collision detection with arrays/lists使用数组/列表进行碰撞检测
【发布时间】:2013-06-13 10:59:44
【问题描述】:

我是碰撞检测的新手,在我的游戏原型中工作时遇到了一些麻烦。在这呆了好几天。经过一些阅读和教程后,我仍然不明白我做错了什么,所以这里的其他人应该能够解释我做错了什么。

游戏运行的 ATM 有 10 个绿色矩形 (npcs) 和 1 个红色矩形 (vampire) 奇怪的屏幕。作为一个起点,我希望吸血鬼通过愚蠢的运气随机与 npc 相交时“喝血”。如果我不注释掉我的碰撞检测方法,我会在代码中得到一个 nullpointexception。

public class Screen 扩展 JPanel 实现 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){}
    }

}

}

公共类 Npc 扩展矩形{

剪辑

public Rectangle bounds(){
    return (new Rectangle (x, y, npcSize, npcSize));
}

剪辑

    }
}

}

公共类 Vamp 扩展矩形{

剪辑

public Rectangle bounds(){
    return (new Rectangle (x, y, npcSize, npcSize));
}

}

【问题讨论】:

  • NullPointerException 与碰撞检测无关,当您尝试尊重未初始化或已初始化为 null 的对象时会发生这种情况。我建议您使用调试器单步执行您的代码,看看发生了什么。
  • 您能否仅隔离演示问题所需的代码?
  • 我剪掉了 npc 和 vamp 类来帮助眼睛。不想减少屏幕太多,就像马特说我没有初始化/为空的东西,我担心我可能会拿出一些可能对我的问题很关键的东西。 (对于 nullpointerexceptions 或调试器仍然不是很好,因为这是我要编写的第二个程序)

标签: java arrays collision-detection


【解决方案1】:

TY 马特。我对 nullpointerException 错误做了一些额外的研究。用新鲜的眼光查看我的代码解决了我的问题。我把我的 checkcollision() 放在我的代码的早期。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多