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'm currently re working an editor got from a tutorial, but I'm having a problem.

I'm making a call to an associative class for the form, which is supposed to get the original tile value and the set a new value, e.g

9(the original tile value) 9#5(the new tile value, based off the previous value)

The trouble I'm having instead of creating the new value it creates this instead 9#5#5#5#5#5#5#5

here is how I'm currently calling the functions

tileNumber = currentCollisionLayer.GetCellIndex(collideCellX, collideCellY);
string TileName = tileNumber + "#" + numForm.actualNumber.Text;
currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, TileName);

when I call either of the other functions alone it works fine, its only when I try and use them in combination there is an issue

The class I calling these functions are below, any help with this would be greatly appreciated.

public class CollisionLayer
    {

        List<Texture2D> collisionTextures = new List<Texture2D>();
        string[,] map;

        public int Width
        {
            get { return map.GetLength(1); }
        }


        public int Height
        {
            get { return map.GetLength(0); }
        }

        public CollisionLayer(int width, int height)
        {
            map = new string[height, width];

            for (int x = 0; x < width; x++)
                for (int y = 0; y < height; y++)
                    map[y, x] = "0";
        }

        #if !XBOX
        public void Save(string filename)
        {
            XmlDocument doc = new XmlDocument();

            XmlElement rootElement = doc.CreateElement("CollisionLayer");
            doc.AppendChild(rootElement);

            XmlElement layoutElement = doc.CreateElement("Layout");

            string line = string.Empty;

            for (int y = 0; y < Height; y++)
            {
                string formatString = string.Empty;

                for (int x = 0; x < Width; x++)
                {
                    formatString += map[y, x].ToString() + " ";
                }
                if (y == 0)
                    line += System.Environment.NewLine + formatString + '\r' + '\n';
                else
                    line += formatString + '\r' + '\n';
            }

            layoutElement.InnerText = line;

            XmlAttribute widthAttribute = doc.CreateAttribute("Width");
            XmlAttribute heightAttribute = doc.CreateAttribute("Height");

            widthAttribute.Value = Width.ToString();
            heightAttribute.Value = Height.ToString();

            layoutElement.Attributes.Append(widthAttribute);
            layoutElement.Attributes.Append(heightAttribute);

            rootElement.AppendChild(layoutElement);

            doc.Save(filename);
        }

        #endif

        #if !XBOX

        public static CollisionLayer FromFile(string filename)
        {
            CollisionLayer tileLayer;
            List<List<int>> tempLayout = new List<List<int>>();

            XmlDocument input = new XmlDocument();

            input.Load(filename);

            int width = 0;
            int height = 0;

            tileLayer = new CollisionLayer(width, height);

            foreach (XmlNode node in input.DocumentElement.ChildNodes)
            {
                if (node.Name == "Layout")
                {
                    width = int.Parse(node.Attributes["Width"].Value);
                    height = int.Parse(node.Attributes["Height"].Value);

                    tileLayer = new CollisionLayer(width, height);

                    string layout = node.InnerText;

                    string[] lines = layout.Split('\r', '\n');

                    int row = 0;

                    foreach (string line in lines)
                    {
                        string realLine = line.Trim();

                        if (string.IsNullOrEmpty(line))
                            continue;

                        string[] cells = realLine.Split(' ');

                        for (int x = 0; x < width; x++)
                        {
                            string cellIndex = cells[x];
                            tileLayer.SetCellIndex(x, row, cellIndex);
                        }

                        row++;
                    }
                }
            }

            return tileLayer;
        }

        #endif

        public void SetCellIndex(int x, int y, string cellIndex)
        {
            map[y, x] = cellIndex;
        }      

        public void SetCellIndex(Point point, string cellIndex)
        {
            map[point.Y, point.X] = cellIndex;
        }

        public string GetCellIndex(int x, int y)
        {
            return map[y, x];
        }

        public string GetCellIndex(Point point)
        {
            return map[point.Y, point.X];
        }

        //public void RemoveIndex(string existingIndex)
        //{
        //    for (int x = 0; x < Width; x++)
        //    {
        //        for (int y = 0; y < Height; y++)
        //        {
        //            if (map[y, x] == existingIndex)
        //                map[y, x] = "0";
        //            else if (map[y, x] > existingIndex)
        //                map[y, x]--;
        //        }
        //    }
        //}

        public void ReplaceIndex(string existingIndex, string newIndex)
        {
            for (int x = 0; x < Width; x++)
                for (int y = 0; y < Height; y++)
                    if (map[y, x] == existingIndex)
                        map[y, x] = newIndex;
        }
    }
}
share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

I'd recommend either using the debugger (to set a breakpoint on the 'set' function) or add some debug output (in the 'set' function and a few other places), to see what's going on. To get '9#5#5#5#5#5#5#5' the three lines of code you have to modify the existing value (and add on '#' plus the new value) must be getting called 7 times.

The debugger should be the easiest method of finding out what's going on.

share|improve this answer
add comment

Since it looks like the only place you're adding a # to your string is in the lines you say you're using to call the functions in your class, I'd say the issue isn't in your class functions.

string TileName = tileNumber + "#" + numForm.actualNumber.Text;

The line above is getting called more than you want it to. The previous answer is excellent, use your debugger and place a break point on the line I mentioned above. I imagine you'll find that it's your problem. This site should give you basic debugging skills if you're not sure how to use the Visual Studio debugger.

share|improve this answer
    
@Roger Perkins- I'd plus 1 your answer but I don't have the rep on this site yet, sorry! –  Byte56 May 10 '11 at 0:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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