简体   繁体   English

XNA:在数组中存储大量Texture2D

[英]XNA: Storing lots of Texture2D in an array

I'm starting out with xna, I'm pretty newbie with this, but I'm making my efforts to go on with this framework, anymay, my problem is: that I have many .png images and dont want to make an object for any of those images so I want to put them up in a Texture2D array, I thought that this is way to do it, but looks like it's not the correct way: 我从xna开始,我是一个新手,但是我正在努力继续使用该框架,也许,我的问题是:我有很多.png图像并且不想创建对象对于这些图像中的任何图像,我想将它们放在Texture2D数组中,我认为这是做到这一点的方法,但是看起来这不是正确的方法:

Texture2D[] _rCards, _bCards, _sCards;
_bCards = new Texture2D[9]; 
_rCards = new Texture2D[9];
_sCards = new Texture2D[6];

for (int i = 1; i < 10; i++)
{
    _bCards[i] = Content.Load<Texture2D>("Images/Common/Black/"+i);
    _rCards[i] = Content.Load<Texture2D>("Images/Common/Red/"+i);
    if(i<6)
        _sCards[i] = Content.Load<Texture2D>("Images/Special/Card" + (i-1));
}

The file names for the texture are 1.png, 2.png, 3.png, and so on. 纹理的文件名是1.png,2.png,3.png等。

For the special cards are card1.png, card2.png,card3.png and so on. 对于特殊卡,包括card1.png,card2.png,card3.png等。

I'm trying to make a blackjack game. 我正在尝试制作二十一点游戏。

Can you give me an advice to load all this textures in one single texture2D array. 您能给我一个建议吗,将所有这些纹理加载到一个单个texture2D数组中。

The IDE gives an NULLREFERENCEEXCEPTION issue or something. IDE给出了NULLREFERENCEEXCEPTION问题或其他问题。

Maybe the language doesnt understands the entire adress to find the textures as a string. 也许该语言不了解整个地址,无法以字符串形式找到纹理。

Indexes are 0 based... 索引从0开始...

for (int i = 1; i < 10; i++)
{
  _bCards[i-1] = Content.Load("Images/Common/Black/"+i);
  _rCards[i-1] = Content.Load("Images/Common/Red/"+i);
   if(i<6) _sCards[i-1] = Content.Load("Images/Special/Card" + (i-1));
}

if you want to load all textures at same time you can use the sprite sheet sample: 如果要同时加载所有纹理,则可以使用精灵表样本:

http://create.msdn.com/en-US/education/catalog/sample/sprite_sheet http://create.msdn.com/zh-CN/education/catalog/sample/sprite_sheet

You will have an unique asset and a dictionary of source rectangles to draw the sprites... 您将拥有一个独特的资源和一个源矩形字典来绘制精灵。

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

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