TOMG's Blog


XNA – Tile Grid FAQ’s – 1: Why is my Tile Map at a 90 degree angle?

In regards to the tile, grid-based game I’m working on, Dungeon Runner , I get asked a lot of questions. However, these tend to be the same questions, asked by different people. As a response to this, I’m going to start posting some answers! So here’s question, and answer, 1!

Question 1: “My grid gets displayed all wrong!” Yes, I get asked questions as unspecific as this. What you’re trying to ask is “Why is my grid being displayed at a weird 90 degree angle?!”

Answer 1: It’s to do with how C# as a language index’s grids. Basically, the X and Y become reversed. This means that Dimension (0) is Y, and Dimension (1) is X, not the other way round. So the only change you need to make to your code is to reverse these two, as I’ve done below;

for (int x = 0; x < tileMapVariable.GetLength(1); x++)
{
     for(int y = 0; y < tileMapVariable.GetLength(0); y++)
     {
          //do something to tileMapVariable[x,y]
     }
}

As you can see, X references GetLength(1), and Y the opposite.

N.B I’m not completely sure GetLength is actually a function, but you get what I mean.