1

我正在开发一个使用第三方库的项目,这似乎给我带来了一些麻烦。

该项目是一个使用 MonoGame 和 FarseerPhysics 的游戏。

导致问题的代码(下面的github):

    private void DrawDebug(GameTime gameTime, RenderBatch renderBatch)
    {
        AABB boundingBox = GetBoundingBox();
        Vector2 tl = new Vector2(boundingBox.LowerBound.X, boundingBox.LowerBound.Y);
        Vector2 tr = new Vector2(boundingBox.LowerBound.X, boundingBox.UpperBound.Y);
        Vector2 bl = new Vector2(boundingBox.UpperBound.X, boundingBox.LowerBound.Y);
        Vector2 br = new Vector2(boundingBox.UpperBound.X, boundingBox.UpperBound.Y);

        Color drawColor = Color.White * 0.4f;
        renderBatch.Queue(new DrawLineInstruction(tl, tr, 0) { Color = drawColor });
        renderBatch.Queue(new DrawLineInstruction(tl, bl, 0) { Color = drawColor });
        renderBatch.Queue(new DrawLineInstruction(tr, br, 0) { Color = drawColor });
        renderBatch.Queue(new DrawLineInstruction(bl, br, 0) { Color = drawColor });
    }

    private AABB GetBoundingBox()
    {
        AABB boundingBox;
        FarseerPhysics.Common.Transform transform = GetFarseerTransform();
        BoundingShape.ComputeAABB(out boundingBox, ref transform, 0);
        //Weird edit-and-continue bug where properties are duplicated after editing this class
        return new AABB() { LowerBound = boundingBox.LowerBound, UpperBound = boundingBox.UpperBound };
    }

我放了一个断点Color drawColor = Color.White * 0.4f;并将浮点值更改为其他值。当我继续创建 NullReferenceException 时BoundingShape.ComputeAABB(...)。它还显示了 2 个称为 BoundingShape 的属性和 2 个字段 _shape(这是 BoundingShape 的支持字段)。

这是我的错还是我可以解决的问题?

这是一个截图。注意本地人的观点,BoundingShape 已经被复制了。此外,Entity(包含正在编辑的代码的类)是抽象的,Level 完全实现了抽象类。

奇怪的部分:

它必须与 out 和 ref 参数有关,因为如果我在数组中返回 LowerBound 和 UpperBound 则它可以工作,但如果我返回 AABB 实例则它会失败。

我尝试过的事情:

  • 直接使用 BoundingShape 的 backing 字段(没有结果,backing 字段也是重复的,见截图)

  • 将边界框代码放在它自己的方法中(没有结果,仅在将返回类型更改为数组时)。

  • 不在 foreach 循环中更改代码Microsoft.Xna.Framework.Game.SortingFilteringCollection.ForEachFilteredItem(System.Action action, Microsoft.Xna.Framework.GameTime userData)

除了我有的解决方法;是否有一些视觉工作室设置导致了这种情况?

Github 仓库

感谢您阅读我的问题

4

0 回答 0