Question

I have been working on a basic tile engine for a project of mine and have managed to get it to a fairly functional state. However one main problem is that when I run the code, the tiles will always start to draw at (16, 0) rather than (0, 0):

        int row = 0, column = 0;
        for (int x = 1; x < array.Length; x++) {
            if (x % 32 == 0 && x != 0)
                row++;
            if (column >= 31)
                column = 0;
            else
                column++;

            Tile newTile = new Tile();
            newTile.tileSheet = tileSheet;
            newTile.tilePos = new Vector2(column * 16, row * 16);

            if (array[x] == Color.Black) {
                newTile.tileType = 0;
                newTile.tileSource = new Rectangle(0, 0, 16, 16);
            }
            else {
                newTile.tileType = 1;
                newTile.tileSource = new Rectangle(16, 0, 16, 16);
            }

            tileList.Add(newTile);
        }

And a picture of the problem:

enter image description here

I know I start at x = 1 for my for() loop, but even when counteracting this with if ((x - 1) ...) it does not work. I am really stumped.

Was it helpful?

Solution

That's because you say

if (column >= 31)
    column = 0;
else
    column++;

Because of this, the first column is 1.

I wonder, why you do the loop this way. It is really hard to read and even harder to debug as you learned. Why don't you use nested for loops?

for(int row = 0; row < height; ++row)
    for(int column = 0; column < width; ++column)
    {
        //add the tiles
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top