XNA 列表与列表之间的差异和 List[] 当您尝试创建它们时

发布于 2024-12-05 00:23:06 字数 1127 浏览 1 评论 0原文

所以,我有一个向量列表,当我在其中放入一个新向量时,它工作得很好,但是如果我尝试访问向量列表的索引并在其中放入一个向量,我会收到此错误“对象引用未设置为一个对象的实例。”每个列表的代码几乎相同:

class GameMap
{
    MouseHandler mouseHandler;
    TileSet tileSet;
    List<Vector2>[] tiles;
    List<Vector2> tile;

    public GameMap(ContentManager Content)
    {
        mouseHandler = new MouseHandler();
        tileSet = new TileSet(Content);
    }

    public void Initialize()
    {
        tiles = new List<Vector2>[tileSet.tiles]; //What am I doing wrong here?
        tile = new List<Vector2>();
    }

    public void MapEditor()
    {
        mouseHandler.MouseUpdate();

        if (mouseHandler.LeftButton == true)
        {
            tiles[0].Add(mouseHandler.MousePosition); //The error comes out here.
            tile.Add(mouseHandler.MousePosition);
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int i = 0; i < tileSet.tiles; i++)
        {
            tileSet.TiledTexture(spriteBatch, tiles[i], i);
            tileSet.TiledTexture(spriteBatch, tile, i);
        }
    }
}

具有多个向量列表的“平铺”不起作用,我做错了什么?

So, I have a list of vectors, when I put a new vector in it it works perfectly, but If I try to access an index of List of Vectors and put a vector in it, I get this error "Object reference not set to an instance of an object." The code is almost the same for each List:

class GameMap
{
    MouseHandler mouseHandler;
    TileSet tileSet;
    List<Vector2>[] tiles;
    List<Vector2> tile;

    public GameMap(ContentManager Content)
    {
        mouseHandler = new MouseHandler();
        tileSet = new TileSet(Content);
    }

    public void Initialize()
    {
        tiles = new List<Vector2>[tileSet.tiles]; //What am I doing wrong here?
        tile = new List<Vector2>();
    }

    public void MapEditor()
    {
        mouseHandler.MouseUpdate();

        if (mouseHandler.LeftButton == true)
        {
            tiles[0].Add(mouseHandler.MousePosition); //The error comes out here.
            tile.Add(mouseHandler.MousePosition);
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int i = 0; i < tileSet.tiles; i++)
        {
            tileSet.TiledTexture(spriteBatch, tiles[i], i);
            tileSet.TiledTexture(spriteBatch, tile, i);
        }
    }
}

The "Tiles" with multiples Lists of vectors isn't working, what am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生未歇 2024-12-12 00:23:06
tiles = new List<Vector2>[tileSet.tiles]

使用 tileSet.tiles 元素创建一个新的 List 数组。这些元素最初为空。当您访问tiles[0].Add(...)时,tiles[0]仍然是null,因此您得到一个null参考异常。

您需要执行与此类似的操作:

tiles = new List<Vector2>[tileSet.tiles]
for (int i = 0; i < tiles.Length; ++i)
{
     tiles[i] = new List<Vector2>();
}

编辑:

我想重点是:

虽然

tile = new List<Vector2>()

tiles = new List<Vector2>[tileSet.tiles]

看起来非常相似,但它们却截然不同。第一个创建一个新的 List,第二个创建一个 List 引用数组,该数组最初为 null。您仍然必须先将每个引用初始化为有效列表,然后才能使用它。

tiles = new List<Vector2>[tileSet.tiles]

creates a new array of List<Vector2> with tileSet.tiles elements. Those elements are null initially. When you access tiles[0].Add(...), tiles[0] is still null, and thus you get a null reference exception.

You need to do something similar to this:

tiles = new List<Vector2>[tileSet.tiles]
for (int i = 0; i < tiles.Length; ++i)
{
     tiles[i] = new List<Vector2>();
}

EDIT:

I guess the point is this:

Although

tile = new List<Vector2>()

and

tiles = new List<Vector2>[tileSet.tiles]

look very similar, they are quite different. The first creates a new List, and the second creates an array of List references, which are initially null. You still have to initialize each of those references to a valid list before you can use it.

甩你一脸翔 2024-12-12 00:23:06

tiles[0].Add(mouseHandler.MousePosition);

尽管您已经创建了一个数组来包含一堆列表,但您必须实例化该数组中的每个列表。

//something like this:
tiles[0] = new List<Vector2>();
tiles[1] = new List<Vector2>();
...
tiles[n] = new List<Vector2>();

您想用列表数组包含什么?可能有一种不太嵌套/复杂的方法来实现您的目标。

tiles[0].Add(mouseHandler.MousePosition);

Although you've created an array to contain a bunch of Lists, You have to instantiate each List in that array.

//something like this:
tiles[0] = new List<Vector2>();
tiles[1] = new List<Vector2>();
...
tiles[n] = new List<Vector2>();

What are you trying to contain with an array of lists? Possibly there is a less nested/complex way to accomplish your goal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文