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

Class question

Last post 2/20/2008 2:12 PM by Swordshock. 5 replies.
  • 2/19/2008 3:15 PM

    Class question

    This has been driving me insane, I hate to post dumb questions on here, its a syntax thing...and its clearly pretty basic! I have been struggling with this for more time than I really ever should have! but I am soooo frustrated I want to kill something!

    Right I have a class, I want to add new things to it, just new unnamed generic objects (imagine there are 30 aliens flying around, then I decide I want to spawn another 10...)

    In this example I wanted to just create a new "Thing" every game loop then draw it, just to make sure I can actually make new things, I cant! (look for the fat line of /////////////// for the bit that matters!)

    so heres my code (which doesnt compile)

    public class Game1 : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
            Random myRand = new Random();
    //I am guessing I need this next line!/////////////////////
            Thing[] thing;
            Texture2D SpriteImage;

            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
                graphics.PreferredBackBufferWidth = 853;
                graphics.PreferredBackBufferHeight = 480;
            }

        
            protected override void Initialize()
            {
                base.Initialize();
            }

            protected override void LoadContent()
            {
                SpriteImage = Content.Load<Texture2D>("Sprite");
                spriteBatch = new SpriteBatch(GraphicsDevice);
            }
            protected override void UnloadContent()
            {
            }

            protected override void Update(GameTime gameTime)
            {
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    this.Exit();

    //the important bit!!!!///////////////////////////////////////////////////////////////////////////////////////////////////////
                x = new Thing();
                x.location = new Vector2 ((int) myRand.Next(0, 853), (int) myRand.Next(0, 480));
                base.Update(gameTime);
            }

            protected override void Draw(GameTime gameTime)
            {
                graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

                foreach (Thing things in thing)
                {
                    spriteBatch.Draw(SpriteImage, things.Location, Color.White);
                }

                base.Draw(gameTime);
            }
        }
    }

    and here is my class file:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    namespace WindowsGame1
    {
        class Thing
        {
            public Vector2 Location;
        }
    }

    I understand the concept behind classes, but so far I have only been creating them with -
    box = new Box[BoxCount];
                for (int index = 0; index < BoxCount; index++)
                {
                    box[index] = new Box()

    and I then what ever I do I dont seem to be able to add any more objects to that initial volume.

    If anyone can help I'll be forever in your debt (it really is annoying me that much!)
  • 2/19/2008 3:31 PM In reply to

    Re: Class question

    Well, your code isn't compiling because you've never declared your "x" object. You would have to change the line where you're declaring a new "thing" to look like this.

    Thing x = new Thing();

    Now your code should compile without errors, but you're still not going to be adding an new items to your array of "thing"s. But that's because you never write any code to do that.

    Personally, I would switch from an array to a list object. So instead of this line

    Thing[] thing;

    use this line

     List<Thing> thing = new List<Thing>();

    now in your updated code where you are trying to create new things every update, you'll want to do that like this

            protected override void Update(GameTime gameTime)
            {
                // TODO: Add your update logic here
                    this.Exit();

                Thing x = new Thing();
                x.Location = new Vector2((int)myRand.Next(0, 853), (int)myRand.Next(0, 480));
                thing.Add(x);

                base.Update(gameTime);
            }


    Your draw code remains the same.

    So what you're doing in that Update code is creating a new object call Thing and then adding it to your list of objects in your class called "thing".

    Hope that helps get you going.

  • 2/19/2008 4:03 PM In reply to

    Re: Class question

    You are a god among men!

    I had been messing with the "list" thing, but I had been putting it in the wrong place. Because it includes the word "new" I had been trying to use it when ever I wanted to make a new object....its all so simple now!

    the- Thing [] thing; seems so innocuous, I didn't think it actually did anything except link the class with the outside world...

    Thanks!
  • 2/19/2008 5:14 PM In reply to

    Re: Class question

    >its a syntax thing...and its clearly pretty basic! I have been struggling with this for more time than I really ever should have! >but I am soooo frustrated I want to kill something!

    - Dude, don't worry, you're certianly not alone in feeling this way! When i get to this point i usually quit for the day... and sometimes see the problem within 30 seconds when i come back to it. (Sometimes not :)

  • 2/19/2008 5:33 PM In reply to

    Re: Class question

    You declared an array of things: Thing[] thing, but you never instantiated it, so the foreach loop would throw a null reference exception. And as George said, you never added the new Things you created into the array.

    so if you change

    Thing[] thing

    to

    const in MaxThings = 30;
    int currentThingCount = 0;
    Thing[] thing = new Thing[MaxThings];

    and in Update

    if (currentThingCount < MaxThings - 1)
    {
       Thing x = new Thing();
       thing[currentThingCount++] = x;
       // setup x location here
    }

    that should work.

    but as George said, Lists are better in that you don't need to manage their size;

    so above could be rewritten as

    List<Thing> things = new List<Thing>();

    and in Update

    Thing x = new Thing();
    things.Add(x);

    which is less code (3 lines instead of 8), easier to read, and less prone to bugs.

    p.s., i think it's better to name arrays, lists etc using plural (things rather than thing) as it more natural language.

    Game hobbyist hell-bent on coding a diabolical Matrix
  • 2/20/2008 2:12 PM In reply to

    Re: Class question

    I advise naming collections of objects plurally.

    Example:  Instead of

       List<Thing> thing;

    try

       List<Thing> things;.

    Maybe this isn't worth saying... :P

    EDIT:

    Craig Martin:
    p.s., i think it's better to name arrays, lists etc using plural (things rather than thing) as it more natural language.

    Apparently you thought it was worth saying too. Haha...

Page 1 of 1 (6 items) Previous Next