Sign in to follow this  

Draw all objects in a list

This topic is 2012 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:

[CODE]
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);
}
}
[/CODE]


My objects class is simple only holds 2 variables:

[CODE]
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);
}
}
[/CODE]


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? Edited by Monkeypoop

Share this post


Link to post
Share on other sites
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. Edited by Jebbles

Share this post


Link to post
Share on other sites
[quote name='Monkeypoop' timestamp='1339462447' post='4948353']
I'm still going to post to see if there is a better way to do this?
[/quote]

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:

[CODE]
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;
}
[/CODE]
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. Edited by BCullis

Share this post


Link to post
Share on other sites

This topic is 2012 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this