Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


Monkeypoop

Member Since 11 Jun 2012
Offline Last Active Jun 13 2012 12:03 PM

Topics I've Started

Draw all objects in a list

11 June 2012 - 06:54 PM

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?

PARTNERS