• Content count

  • Joined

  • Last visited

Community Reputation

102 Neutral

About Monkeypoop

  • Rank
  1. 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?