CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2012
    Location
    england
    Posts
    7

    Conversion from char to int

    Hi,
    I'm experimenting with using a tile based system for a game, the colour of the tiles are based off the int which is found when converting a char array storing the integer found in a text file. 0 is a black tile, 1 is a green tile and 2 is a new line.

    This is the code which places the tiles:
    Code:
       public void createMap() {
    
                for (int y = 0; y <= 5; y++ )
                {
    
                    for (int x = 0; int.Parse(readMapArray[tileIndex].ToString()) < 2; x++ )
                    {
                        tileIndex++;
                        createTile(int.Parse(readMapArray[tileIndex].ToString()));
                        tilePoint = new Point(tilePoint.X + 32, tilePoint.Y);
                    }
                    tileIndex++;
                    tilePoint = new Point(0, tilePoint.Y + 32);
                }
    
            }
    I get a "Format exception was unhandelled" error on the line when the second for loop initialises.
    I only get the error when i include the second incrementation of tileIndex. however, if i take this out, it only creates the first line of tiles (because it doesn't increment the next index past the value of 2).

    This is my code to change the colour of the tile (the createTile):

    Code:
     public void createTile(int tileType) {
                PictureBox tile = new PictureBox();
                tile.Location = tilePoint;
                tile.Height = 32;
                tile.Width = 32;
                tile.Visible = true;
                if (tileType == 0)
                {
                    tile.BackColor = Color.Black;
                }
                if (tileType == 1) {
                    tile.BackColor = Color.GreenYellow;
                }
                this.Controls.Add(tile);
                
            }
    This problem actually kept me awake for hours last night just thinking about it.
    Why is this error happening and what can I do to solve it?

    Thanks in advance

    Arrokai

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Conversion from char to int

    Can you show us where you initialize readMapArray?
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Oct 2012
    Location
    england
    Posts
    7

    Re: Conversion from char to int

    I solved it, I was using a text file which was converted into a char array and then converted that into an integer in the loops posted above. Instead I just converted the text file straight from string to Int array.
    I have no idea why I didn't do this in the first place but for the sake of really wanting to know why it didn't work anyway,

    This was my code for converting the text file:

    Code:
    //reads the text file and 
            public void readMap(String map)
            {
                srMapReader = new StreamReader("Maps//"+map+".txt");//gets map
                string readMap = srMapReader.ReadToEnd();//reads
                readMapArray = readMap.ToCharArray();
                srMapReader.Close();//ends stream
            }
    Thanks for the reply,
    Arrokai

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Conversion from char to int

    Glad you got it working!
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured