Jump to content
  • Advertisement
Sign in to follow this  
Hartley

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

This topic is 3415 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!