Reputation: 49
first of all, i am new to xna. Trying to craete blackjack game. And i have created 2 lists to add random card to the list. But i cannot understand why doesnt it add pics to the list tekstuur2
Heres code :
public class Kaart
{
public Vector2 asukoht = new Vector2(0, 0);
public List<Texture2D> tekstuur = new List<Texture2D>();
public List<Texture2D> tekstuur2 = new List<Texture2D>();
Random rand = new Random();
public void loadContent2(ContentManager manager2)
{
for (int x = 3; x < 7; x++)
tekstuur2.Add(manager2.Load<Texture2D>("Risti" + x.ToString()));
}
public void loadContent(ContentManager manager)
{
for (int j = 3; j < 7; j++)
tekstuur.Add(manager.Load<Texture2D>("Risti" + j.ToString()));
}
public void Draw(SpriteBatch sprite)
{
sprite.Draw(tekstuur[rand.Next(tekstuur.Count)], asukoht, Color.White);
sprite.Draw(tekstuur2[rand.Next(tekstuur2.Count)], asukoht, Color.White);
// an error occurs (ArgumentOutofRangeException) , tekstuur2.count = 0 at that point, but at the tekstuur.Count it is 4.
}
}
}
Heres another class where these methods are called :
namespace WindowsGame1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Vector2 koht = new Vector2(0,0);
Kaart yks;
Kaart kaks;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
yks = new Kaart();
kaks = new Kaart();
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
//kaardi laadimine
yks.loadContent(this.Content);
kaks.loadContent2(this.Content);
// yhe kaarti asukoht
yks.asukoht.X = 100;
yks.asukoht.Y = 300;
// teise kaardi asukoht
kaks.asukoht.X = 200;
kaks.asukoht.Y = 400;
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
// kaartide joonistamine
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
yks.Draw(this.spriteBatch);
kaks.Draw(this.spriteBatch);
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}
Upvotes: 0
Views: 191
Reputation: 2385
ok the problem is you should call for every instance of Kaart (in your case yks and kaks) loadContent AND loadContent2 this in your method LoadContent:
//kaardi laadimine
yks.loadContent(this.Content);
kaks.loadContent2(this.Content);
become:
//kaardi laadimine
yks.loadContent(this.Content);
yks.loadContent2(this.Content);
kaks.loadContent2(this.Content);
kaks.loadContent(this.Content);
note that since the texture are the same there's no need to use two different list for containing them, you can do this in your Kaart Draw method:
sprite.Draw(tekstuur[rand.Next(tekstuur.Count)], asukoht, Color.White);
sprite.Draw(tekstuur[rand.Next(tekstuur.Count)], asukoht, Color.White);
this way you have to only call LoadContent.
Another note: at the moment you're instantiating a Random rand; the problem with that is that sequential instantiation of this class (in a short time) is not recommended and your two KArt are going to use the same random texture. To resolve this make the Random rand a static variable:
private static Random rand = new Random();
Upvotes: 1