Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've started writing a tile based game in XNA today using the tutorials here. I'm on the Part 6 - Side Topic - Auto Transitions at the moment. My transitions aren't working correctly as you can see below:

enter image description here

When moving around, several of the tiles don't transitions correctly and stay as the default square, you can see this happening with the 2 bottom right 'water' tiles, they're not transitioning with the 'dark grass' tiles.

Here's the class that does the transitioning:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsGame1
{
    class TileMap
    {
    public List<MapRow> Rows = new List<MapRow>();
    public int MapWidth = 32;
    public int MapHeight = 32;

    public TileMap()
    {
        for (int y = 0; y < MapHeight; y++)
        {
            MapRow thisRow = new MapRow();
            for (int x = 0; x < MapWidth; x++)
            {
                thisRow.Columns.Add(new MapCell(64));
            }
            Rows.Add(thisRow);
        }

        Rows[0].Columns[3].TileID = 96;
        Rows[0].Columns[4].TileID = 96;
        Rows[0].Columns[5].TileID = 32;
        Rows[0].Columns[6].TileID = 32;
        Rows[0].Columns[7].TileID = 32;

        Rows[1].Columns[3].TileID = 96;
        Rows[1].Columns[4].TileID = 32;
        Rows[1].Columns[5].TileID = 32;
        Rows[1].Columns[6].TileID = 32;
        Rows[1].Columns[7].TileID = 32;

        Rows[2].Columns[2].TileID = 96;
        Rows[2].Columns[3].TileID = 32;
        Rows[2].Columns[4].TileID = 32;
        Rows[2].Columns[5].TileID = 32;
        Rows[2].Columns[6].TileID = 32;
        Rows[2].Columns[7].TileID = 32;

        Rows[3].Columns[2].TileID = 96;
        Rows[3].Columns[3].TileID = 32;
        Rows[3].Columns[4].TileID = 32;
        Rows[3].Columns[5].TileID = 0;
        Rows[3].Columns[6].TileID = 0;
        Rows[3].Columns[7].TileID = 0;

        Rows[4].Columns[2].TileID = 96;
        Rows[4].Columns[3].TileID = 32;
        Rows[4].Columns[4].TileID = 32;
        Rows[4].Columns[5].TileID = 0;
        Rows[4].Columns[6].TileID = 0;
        Rows[4].Columns[7].TileID = 0;

        Rows[5].Columns[2].TileID = 96;
        Rows[5].Columns[3].TileID = 32;
        Rows[5].Columns[4].TileID = 32;
        Rows[5].Columns[5].TileID = 0;
        Rows[5].Columns[6].TileID = 0;
        Rows[5].Columns[7].TileID = 0;

        doAutoTransition();
    }

    private void doAutoTransition()
    {
        for (int y = 0; y < MapHeight; y++)
        {
            for (int x = 0; x < MapWidth; x++)
            {
                int height = getBaseTile(y, x);
                int start = GetTileBaseHeight(height) + 1;
                for (int i = start; i < 4; i++)
                {

                    int tileID = CalculateTransistionTileEdge(y, x, i);
                    if (tileID > -1)
                    {
                        Rows[y].Columns[x].AddBaseTile(i * 32 + tileID);
                    }

                    tileID = CalculateTransistionTileCorner(y, x, i);
                    if (tileID > -1)
                    {
                        Rows[y].Columns[x].AddBaseTile(i * 32 + 16 + tileID);
                    }
                }
            }
        }
    }

    private int GetTileBaseHeight(int tileID)
    {       
        return tileID / 32;
    }

    private int getBaseTile(int y, int x)
    {
        if (x < 0 || y < 0 ||
            x >= MapWidth || y >= MapHeight)
        {
            return 0;
        }

        return Rows[y].Columns[x].TileID;
    }

    private int CalculateTransistionTileEdge(int y, int x, int iHeight)
    {
        int temp = 0;

        if (GetTileBaseHeight(getBaseTile(y, x - 1)) == iHeight)
        {
            //Left
            temp += 1;
        }

        if (GetTileBaseHeight(getBaseTile(y - 1, x)) == iHeight)
        {
            //Top
            temp += 2;
        }

        if (GetTileBaseHeight(getBaseTile(y, x + 1)) == iHeight)
        {
            //Right
            temp += 4;
        }
        if (GetTileBaseHeight(getBaseTile(y + 1, x)) == iHeight)
        {
            //bottom
            temp += 8;
        }

        if (temp > 0)
        {
            return temp;
        }

        return -1;
    }

    private int CalculateTransistionTileCorner(int y, int x, int iHeight)
    {
        int temp = 0;

        if (GetTileBaseHeight(getBaseTile(y - 1, x - 1)) == iHeight)
        {
            //Left top
            temp += 1;
        }
        if (GetTileBaseHeight(getBaseTile(y - 1, x + 1)) == iHeight)
        {
            //Top right
            temp += 2;
        }
        if (GetTileBaseHeight(getBaseTile(y + 1, x + 1)) == iHeight)
        {
            //Bottem Right
            temp += 4;
        }
        if (GetTileBaseHeight(getBaseTile(y + 1, x - 1)) == iHeight)
        {
            //Bottom left
            temp += 8;
        }
        if (temp > 0)
        {
            return temp;
        }

        return -1;
    }
}

class MapRow
{
    public List<MapCell> Columns = new List<MapCell>();
   }
}
share|improve this question

closed as too localized by Sean Middleditch, Trevor Powell, Josh Petrie, Jimmy, Byte56 Feb 5 '13 at 0:48

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
"Debug my code for me" questions are really too localised for this site; we're trying to generate an eternal knowledge-base which will be useful for Google searches. For personal help like what you're asking for here, our chat or a discussion forum like gamedev.net are better places to ask. :) –  Trevor Powell Feb 4 '13 at 4:26

1 Answer 1

up vote 0 down vote accepted

Turns out I had the wrong SpriteSortMode when drawing the tiles to the screen. Changed it to SpriteSortMode.Immediate and works correctly now.

share|improve this answer
2  
You should not use SpriteSortMode.Immediate like this - it does as little batching as possible and will surely cause you a lot of performance gripes in the future. I'd highly advise you try Defered first and if this does not work check out your drawing order and re-arrange it in a way that will give the correct behavior while still retaining performance. –  Vaughan Hilts Feb 2 '13 at 23:45
    
Deferred works just as well, thanks for the heads up! –  Thomas Owers Feb 3 '13 at 0:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.