C# Issue.. How do I? (XNA)

Started by
3 comments, last by Hartley 14 years, 11 months ago
Alright so Im making a Final-Fantasy tactics style game, and I have built the grid, using a multidimensional array, right now its 3x3, 32 pixels per 'cell' Im just having problems drawing it, I dont really know where to start. I guess I need to do a foreach loop, loop through my array and Draw the GrassTile with the same position as the cell. heres my code, Any suggestions? (this is a first with OO language for me, so Im really just having problems communicating with the Game1 class' Draw method, with my Grid class's cell array.

Game1.cs

namespace Tactics
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Grid mapOne;

        Texture2D grassTile;

        public const int cellSize = 32; //The cell size, in pixels, used to multiply the offset.

        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()
        {
            mapOne = new Grid();
            graphics.PreferredBackBufferWidth = 96;
            graphics.PreferredBackBufferHeight = 96;
            graphics.ApplyChanges();

            Window.Title = "Tactics";
            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);

            grassTile = Content.Load<Texture2D>("sprites/grass");
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            base.Update(gameTime);
        }

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

            spriteBatch.Begin();

            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

Cell.cs

namespace Tactics
{
    public class Cell
    {
        Vector2 cellPos;
        Vector2 cellLeft;
        Vector2 cellRight;
        Vector2 cellUp;
        Vector2 cellDown;
        Vector2 cellParent;
        int cellType;

        //cell types.
        public const int cellTypeGrass = 1;

        public Cell(Vector2 pos, int type)
        {
            //Initialize all the cell variables.
            cellPos = pos;
            cellType = type;

            //Based on this cells position, find all the other cells locations.
            cellLeft = cellPos; cellLeft.Y -= Game1.cellSize; //to find the cell to the left, you take the current cells position, then subtract 1 offset from Y
            cellRight = cellPos; cellRight.Y += Game1.cellSize; // same as left, except +offset
            cellUp = cellPos; cellUp.X -= Game1.cellSize; //same as others, except - X offset
            cellDown = cellPos; cellDown.X += Game1.cellSize; //same.

            cellParent = Vector2.Zero; //wont be using this till pathfinding.
        }
    }
}

Grid.cs

namespace Tactics
{
    public class Grid
    {
        public const int gridSize = 3;
        public Cell[,] cells = new Cell[gridSize, gridSize];
        private Vector2 tempCellPos = Vector2.Zero;

        public virtual Grid()
        {
            for (int x = 0; x < gridSize; x++)
            {
                for (int y = 0; y < gridSize; y++)
                {
                    tempCellPos.X = (x * 32); tempCellPos.Y = (y * 32); //Making the grid with a multidimensional array
                    //Send the arguments to the constructor for each cell.
                    cells[x, y] = new Cell(tempCellPos, Cell.cellTypeGrass);
                }

            }
        }
    }
}
any help will be appreciated, those are my 3 classes.
Advertisement
The XNA documentation has an article on how to draw a sprite. Use a for loop to iterate over the multidimensional array, as you have done in Grid constructor.

(As for your cell type, I'd recommend using an enumeration rather than constant integers).

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

you might want to put your question in the post title, "how do i..." doesn't really state the problem. "drawing sprites using XNA" would be better
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
The XNA creators club site has a sample on using tiled sprites.

I would also use an enumeration for my tile type and have my cells store a Texture2D of the graphic they should use.

As for drawing, I would probably have a draw method in my cell class that would handle drawing the sprite to the screen in the correct position. Then my grid class would loop through all the cells that it contains to draw them. Then the game class would simply call the grids Draw() method to draw the entire grid onto the screen.
Thanks everyone for your help, I finally got it displaying my grass tile. =P

That was just step one though xD...
trying to make a final-fantasy-tactics-style game, all I can say is its going to be fun. lol

This topic is closed to new replies.

Advertisement