XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

Beginner problem with arrays of objects

Last post 09/11/2009 20:31 by Chris Kitsch. 3 replies.
  • 06/11/2009 21:05

    Beginner problem with arrays of objects

    Hi guys, I'm trying to get grips with XNA and C#, and I'm completely stumped on something.  I'm trying to have an array of Enemy objects inside of my BattleSystem class.  When I run it, it says for line "enemy[0].Load(Content);" that "Object reference not set to an instance of an object."

    I'm pretty sure I'm doing something fundamentally wrong, but I have no idea what.  Any help is greatly appreciated.

    Feel free to point out any bad practices in the code while you're at it.  I'm very new to all of this.

    namespace MyFirstGame1 
        class BattleSystem 
        { 
            private Enemy[] enemy; 
     
            public BattleSystem() 
            { 
                Enemy[] enemy = new Enemy[4]; 
     
                for (int i = 0; i < 4; i++) 
                { 
                    enemy[i] = new Enemy("Snail", 100f, 100f, 2, 20, 20, 4, 2); 
                } 
     
            } 
     
            public void Load(ContentManager Content) 
            { 
                enemy[0].Load(Content); 
            } 
     
            public void Draw(SpriteBatch spriteBatch) 
            { 
            } 
        } 

    And here's my main Game1.cs if you need it.

    namespace MyFirstGame1 
        /// <summary> 
        /// This is the main type for your game 
        /// </summary> 
        public class Game1 : Microsoft.Xna.Framework.Game 
        { 
            GraphicsDeviceManager graphics; 
            SpriteBatch spriteBatch; 
            Font font = new Font(); 
            BattleSystem battleSystem = new BattleSystem(); 
            Player player = new Player(); 
     
            public Game1() 
            { 
                graphics = new GraphicsDeviceManager(this); 
                Content.RootDirectory = "Content"
            } 
     
            /// <summary> 
            /// Allows the game to perform any initialization it needs to before starting to run. 
            /// This is where it can query for any required services and load any non-graphic 
            /// related content.  Calling base.Initialize will enumerate through any components 
            /// and initialize them as well. 
            /// </summary> 
            protected override void Initialize() 
            { 
                // TODO: Add your initialization logic here 
     
                base.Initialize(); 
            } 
     
     
            /// <summary> 
            /// LoadContent will be called once per game and is the place to load 
            /// all of your content. 
            /// </summary> 
            protected override void LoadContent() 
            { 
                // Create a new SpriteBatch, which can be used to draw textures. 
                spriteBatch = new SpriteBatch(GraphicsDevice); 
                font.LoadContent(Content); 
                battleSystem.Load(Content); 
                player.Load(Content); 
     
                // TODO: use this.Content to load your game content here 
            } 
     
            /// <summary> 
            /// UnloadContent will be called once per game and is the place to unload 
            /// all content. 
            /// </summary> 
            protected override void UnloadContent() 
            { 
                // TODO: Unload any non ContentManager content here 
            } 
     
            /// <summary> 
            /// Allows the game to run logic such as updating the world, 
            /// checking for collisions, gathering input, and playing audio. 
            /// </summary> 
            /// <param name="gameTime">Provides a snapshot of timing values.</param> 
            protected override void Update(GameTime gameTime) 
            { 
                // Allows the game to exit 
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
                    this.Exit(); 
     
                // TODO: Add your update logic here 
     
     
                base.Update(gameTime); 
            } 
     
            /// <summary> 
            /// This is called when the game should draw itself. 
            /// </summary> 
            /// <param name="gameTime">Provides a snapshot of timing values.</param> 
            protected override void Draw(GameTime gameTime) 
            { 
                GraphicsDevice.Clear(Color.CornflowerBlue); 
     
                // TODO: Add your drawing code here 
     
                spriteBatch.Begin(SpriteBlendMode.AlphaBlend); 
                font.Draw(spriteBatch); 
                player.Draw(spriteBatch); 
                spriteBatch.End(); 
     
     
                base.Draw(gameTime); 
            } 
        } 

  • 06/11/2009 21:08 In reply to

    Re: Beginner problem with arrays of objects

    You are creating a local variable in your constructor with the same name as your field, thus never creating your class-based enemy array.

      Enemy[] enemy = new Enemy[4];

    Instead you want to be initializing your class field:

      enemy = new Enemy[4];
    www.dadoogames.com
    Curling 2010 - in playtest soon, this month or next, this year for sure (maybe)
  • 06/11/2009 21:13 In reply to

    Re: Beginner problem with arrays of objects

    Right.  I get it now.  Thank you so much, I really appreciate it.  :D
  • 09/11/2009 20:31 In reply to

    Re: Beginner problem with arrays of objects

    I would actually use a List as I find the implementation to be easier to handle. 
    //Declare:
       private List&lt;Enemy&gt; enemy;<br> 
    //Instance:<br> 
       enemy = new List&lt;Enemy&gt;;<br> 
    //Add (within loop):
       enemy.Add(new Enemy(/*All this constructor stuff*/);<br> 
    //Update:
       foreach (Enemy e in enemy)
       {
          e.Update(gameTime);
       }
    //Draw:
       foreach (Enemy e in enemy)
       {
          e.Draw(spriteBatch);
       }

    But to each their own.
Page 1 of 1 (4 items) Previous Next