0

我的 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--;
        }
    }
}

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

4

1 回答 1

1

Do these classes implement Shape? If not, they should. See the answer to Collision detection with complex shapes for an SSCCE.

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

That seems logical to me, from your description of both objects.

..i just type implements Shape?

I would tend to use a Rectangle2D or Rectangle2D.Double for the obstacle & an Ellipse2D or Ellipse2D.Double for the shot. Rather than extend them, just hold them as an instance variable.

Give it a go & let us know how you go. If you get stuck, post an SSCCE of your best attempt.
You might need to hot-link to some small images.

.. ..

于 2013-07-15T06:51:42.750 回答