Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


Draw all objects in a list


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Monkeypoop   Members   -  Reputation: 102

Like
0Likes
Like

Posted 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?

Edited by Monkeypoop, 11 June 2012 - 08:19 PM.


Sponsor:

#2 Jebbs   Members   -  Reputation: 280

Like
0Likes
Like

Posted 11 June 2012 - 08:27 PM

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, 11 June 2012 - 08:32 PM.


#3 BCullis   Crossbones+   -  Reputation: 1813

Like
0Likes
Like

Posted 12 June 2012 - 09:34 AM

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.

Edited by BCullis, 12 June 2012 - 09:37 AM.

Hazard Pay :: FPS/RTS in SharpDX
DeviantArt :: Because right-brain needs love too




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS