XNA 列表- 与列表
- 之间的差异和 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);
}
}
}
具有多个向量列表的“平铺”不起作用,我做错了什么?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
tileSet.tiles
元素创建一个新的List
数组。这些元素最初为空。当您访问tiles[0].Add(...)
时,tiles[0]
仍然是null
,因此您得到一个null参考异常。您需要执行与此类似的操作:
编辑:
我想重点是:
虽然
和
看起来非常相似,但它们却截然不同。第一个创建一个新的 List,第二个创建一个 List 引用数组,该数组最初为 null。您仍然必须先将每个引用初始化为有效列表,然后才能使用它。
creates a new array of
List<Vector2>
withtileSet.tiles
elements. Those elements are null initially. When you accesstiles[0].Add(...)
,tiles[0]
is stillnull
, and thus you get a null reference exception.You need to do something similar to this:
EDIT:
I guess the point is this:
Although
and
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.
尽管您已经创建了一个数组来包含一堆列表,但您必须实例化该数组中的每个列表。
您想用列表数组包含什么?可能有一种不太嵌套/复杂的方法来实现您的目标。
Although you've created an array to contain a bunch of Lists, You have to instantiate each List in that array.
What are you trying to contain with an array of lists? Possibly there is a less nested/complex way to accomplish your goal.