Draw all objects in a list

Started by
1 comment, last by BCullis 11 years, 10 months ago
So I'm looking to draw all objects from a list as the title implies. I would like to add to the list with a button, and then draw the object to the screen in a random location. My coding looks like:


public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
List<objects> allObjects = new List<objects>();
Random rand = new Random();
KeyboardState keyState = new KeyboardState();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
foreach (objects obj in allObjects)
obj.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
foreach (objects obj in allObjects)
obj.LoadContent(Content);
}
protected override void Update(GameTime gameTime)
{
keyState = Keyboard.GetState();
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || keyState.IsKeyDown(Keys.Escape) == true)
this.Exit();
if (keyState.IsKeyDown(Keys.N))
addObject();
base.Update(gameTime);
foreach (objects obj in allObjects)
obj.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
spriteBatch.Begin();
foreach (objects obj in allObjects)
obj.Draw(spriteBatch);

spriteBatch.End();
}
public void addObject()
{
objects newObject = new objects();
newObject.position.X = rand.Next(0, graphics.PreferredBackBufferWidth);
newObject.position.Y = rand.Next(0, graphics.PreferredBackBufferHeight);
allObjects.Add(newObject);
}
}



My objects class is simple only holds 2 variables:


class objects
{
public Vector2 position = new Vector2(0,0);
public Texture2D spriteImage;
public void Initialize(ContentManager Content)
{

}
public void LoadContent(ContentManager Content)
{
spriteImage = Content.Load<Texture2D>("Sprites\\Square");
}
public void Update(GameTime gameTime)
{
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(spriteImage, position, Color.White);
}
}



When I run this and press "n" to create a new object i get an error saying, "This method does not accept null for this parameter.
Parameter name: texture" So I assume that there is some problem with my Initialization. Is there a better way to go about doing this, does the initialization only happen once in main(). If that's the case do I have to just initialize every sprite I'm going to use and then make a copy of it when I create a new object.

While writing that I solved it adding onto the addObject:
allObjects.Add(newObject);
this.Initialize();
this.LoadContent();
I'm still going to post to see if there is a better way to do this?
Advertisement
Well if you know beforehand what textures you are going to use, one option would be to load your textures before hand and them pass them into your add object method.

Alternatively, you could populate your object list before you press n, and call each object's LoadContent in your main LoadContent method before you even enter your main game loop.


Edit:

Thought I should actually answer your question. The reason you were getting the error( which what I posted above would correct) is because your main Initialize and LoadContent methods are called before you have added objects into your list. Basically when you press n you are populating your list of objects but never actually calling either of these methods. That's why you had to make your changes into the code to get it to work.

I'm still going to post to see if there is a better way to do this?


Yes, define a default (and possibly custom) constructor for your "objects" class, instead of using the default that's silently written for you (under the hood).
Something like this would suffice:


public objects()
{
this.position = new Vector2(0,0);
this.spriteImage = Content.Load<Texture2D>("Sprites\\Square");
}

public objects(Vector2 position, Texture2D spriteImage)
{
this.position = position;
this.spriteImage = spriteImage;
}

That provides you both the default (with no arguments) and a constructor that would let you define position and texture at the time of creation (a bit more flexible).

IMHO, it's a good practice to get into to define your constructor(s) for every class you write. Even if it's empty, having one in place gives you somewhere to add code when/if you need to.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

This topic is closed to new replies.

Advertisement