简体   繁体   English

在Texture2D XNA列表中加载纹理

[英]Load a texture in List of Texture2D XNA

I created the 我创建了

List<Texture2D> paddles = new List<Texture2D>();

and in the LoadContent() in the XNA, I wanted to load a single texture so I did it like this: 在XNA的LoadContent()中,我想加载一个纹理,所以我这样做是这样的:

paddles[0] = Content.Load<Texture2D>("Graphics/First Paddle");
paddles[1] = Content.Load<Texture2D>("Graphics/Second Paddle");

but I have received this error at this line of code when I tried to run it paddles[0] = Content.Load<Texture2D>("Graphics/First Paddle"); 但是当我尝试运行它时,在此代码行中收到了此错误paddles[0] = Content.Load<Texture2D>("Graphics/First Paddle");

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll Additional information: Index was out of range. mscorlib.dll中发生了'System.ArgumentOutOfRangeException'类型的未处理异常。其他信息:索引超出范围。 Must be non-negative and less than the size of the collection. 必须为非负数并且小于集合的大小。

How to solve this? 如何解决呢?

Thank you 谢谢

You're trying to reference list items that currently don't exist, and therefore, you're getting the given error. 您正在尝试引用当前不存在的列表项,因此,您遇到了给定的错误。 Try using the below: 尝试使用以下内容:

paddles.Add(Content.Load<Texture2D>("Graphics/First Paddle"));

Note in the future that a list never has its objects instantiated, so you need to use Add to directly add to the list. 请注意,将来列表永远不会实例化其对象,因此您需要使用Add直接添加到列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM