有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

awt Java交叉错误

我的DoLogic方法有这个代码。我试着在投篮和障碍物之间做一个交叉,但我真的什么都想不出来。。因为两者都是不同的对象。。我试着做了一些,但它根本没有检测到什么

for(int i=0; i<shots.length; i++)
{
    if(shots[i] != null)
    {
        shots[i].moveShot(SHOTSPEED);
        if(shots[i].getXPos() > 1280)
        {
            shots[i] = null;
        }
    }
}
for(int i=0; i<obstacles.length; i++)
{
    if(obstacles[i] == null) 
    { 
        obstacles[i] = generateObstacle(); 
        break;
    } 
    if(obstacles[i] != null)
    {
        obstacles[i].moveObstacle(); 
        if(obstacles[i].getXPos() < 10) 
        { 
            obstacles[i] = null; 
        }
        else if(obstacles[i].intersects(Player1.character))
        {
            obstacles[i] = null;
            GameSounds.hit("/resources/8bit_bomb_explosion.wav");
            lives--;
        }
    }
}

你们能给我举个例子,或者至少给我一个建议,如何在障碍物和射击之间进行交叉吗


共 (1) 个答案

  1. # 1 楼答案

    这些类是否实现了^{}?如果没有,他们应该这样做。有关SSCCE,请参见Collision detection with complex shapes的答案

    ..and i should implement the Rectangle in Obstacle and Oval in shot?

    从你对两个物体的描述来看,这似乎是合乎逻辑的

    ..i just type implements Shape?

    我倾向于用^{}^{}表示障碍&;一个^{}^{}的镜头。与其扩展它们,不如将它们作为实例变量保存

    试一试&;让我们知道你进展如何。如果你被卡住了,发布你最好的尝试的SSCCE
    您可能需要热链接到some small images