XNA - 碰撞永远不会发生

时间:2014-12-06 20:30:52

标签: c# xna

我在XNA中创建游戏,我需要一个碰撞检测逻辑:

public Rectangle boundingBox = new Rectangle((int)playerShipPos.X, (int)playerShipPos.Y, frameWidth, frameHeight);

this.boundingBox = new Rectangle((int)meteorPosPub.X, (int)meteorPosPub.Y, (int)meteorTexture.Width, (int)meteorTexture.Height);

for (int i = meteorList.Count - 1; i >= 0; i--)
{
    meteorGenerator meteor = new meteorGenerator(Vector2.Zero);

    if (meteorList[i].meteorPosPub.Y > 664)
    {
        meteorList.RemoveAt(i);
        if (meteor.boundingBox.Intersects(playerShip.boundingBox))
        {
            meteorList.RemoveAt(i);
        }
    }
}

所以我想要达到这样的效果:如果玩家船接触流星,流星会被隐藏并从列表中移除但实际上没有任何反应。

1 个答案:

答案 0 :(得分:0)

for (int i = meteorList.Count - 1; i >= 0; i--)
{
    meteorGenerator meteor = new meteorGenerator(Vector2.Zero);//you are creating new list meteors every frame, this is ridiculous 

    if (meteorList[i].meteorPosPub.Y > 664)
    {
        meteorList.RemoveAt(i);//if you are removing a position from a list, not destroying the meteor
        if (meteor.boundingBox.Intersects(playerShip.boundingBox))
        {
            meteorList.RemoveAt(i);//you already did this, this conditional is unnecessary
        }
    }
}

我不知道你在做什么,但这就是我要做的。

1.让玩家和流星从具有固体对象属性的类继承。

  1. 根据对象类型将这些添加到具有唯一ID的列表中。

  2. 每一帧都要检查身份证(这样可以让您更好地控制想要与之碰撞的内容)。

  3. 继续检查是否存在冲突,如果您要删除元素,只需将其从列表中删除并销毁即可。