【问题标题】:XNA - Collision never happensXNA - 碰撞永远不会发生
【发布时间】:2014-12-07 04:41:01
【问题描述】:

我正在 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);
        }
    }
}

所以我想实现这个效果:如果玩家飞船接触到流星,流星就会隐藏并从列表中删除,但实际上什么也没发生。

【问题讨论】:

    标签: c# xna


    【解决方案1】:
    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. 每一帧,检查 ID(这使您可以更好地控制要与什么发生冲突)。

    3. 继续检查冲突,如果您想删除一个元素,只需将其从列表中删除并销毁即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-16
      • 2013-05-08
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多