简体   繁体   中英

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:

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.

For the special cards are card1.png, card2.png,card3.png and so on.

I'm trying to make a blackjack game.

Can you give me an advice to load all this textures in one single texture2D array.

The IDE gives an NULLREFERENCEEXCEPTION issue or something.

Maybe the language doesnt understands the entire adress to find the textures as a string.

Indexes are 0 based...

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

You will have an unique asset and a dictionary of source rectangles to draw the sprites...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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