Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
So i'm coding a game and to load the tiles, the code will read the .map file where the map info is. But, it gives me this error.

the .map file looks like this:
That's only one line...

48:48:00:06 48:48:00:06 48:00:00:07 1F:00:00:07 22:00:00:07 22:00:00:07 20:00:00:06 22:00:00:06 22:00:00:07 1F:00:00:06 82:00:00:06 87:52:00:06 81:52:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06 00:00:00:06



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace Core
{
    public class Core
    {
        //-----------Core------------//
        //-structs-//
        public struct XY
        {
            public int
                X,
                Y;
            public XY(int x, int y)
            {
                X = x; Y = y;
            }
        }

        //-Static Member-//
        public class Members
        {
            public static Panel VIEW = new Panel
            {
                Location = new Point(100, 0),
                Size = new Size(570, 570),
                Visible = true
            };
            public static Panel WORLD;
            public static Engine ENGINE = new Engine();
            public static Tile[,] TILE = new Tile[T_XCOUNT, T_YCOUNT];
            public static Init INIT = new Init();
            public static int
                T_XCOUNT,
                T_YCOUNT;
            public static bool
                FIRST_TIME_ENGINE_INIT = false,
                PLAYER_RENDER = false;

        }

        //-Player-//
        public class Player
        {
            public static Bitmap TEXTURE;
            public static Rectangle DIMENSION;
            public static XY LOCATION;
            public static char
                PLAYER_LOOKING_AT = 'S';
            public const int
                DEFAULT_XLOC = 9,
                DEFAULT_YLOC = 8,
                DEFAULT_XSIZE = 30,
                DEFAULT_YSIZE = 30;
            public const string
                DEFAULT_SPRITE = @"Sprites/Animated/Character/Gael/Gael-LSW.png";
            public static bool LookingAt(char direction)
            {
                PLAYER_LOOKING_AT = direction;
                if (PLAYER_LOOKING_AT == 'S')
                {
                    TEXTURE = new Bitmap(DEFAULT_SPRITE);
                }
                else if (PLAYER_LOOKING_AT == 'E')
                {
                    TEXTURE = new Bitmap(DEFAULT_SPRITE);
                }
                else if (PLAYER_LOOKING_AT == 'N')
                {
                    TEXTURE = new Bitmap(DEFAULT_SPRITE);
                }
                else if (PLAYER_LOOKING_AT == 'W')
                {
                    TEXTURE = new Bitmap(DEFAULT_SPRITE);
                }
                else
                {
                    TEXTURE = new Bitmap(DEFAULT_SPRITE);
                }
                return true;
            }
        }

        //-Constant Value-//
        #region Const
        public const int
        #region Values
            WINDOW_XSIZE = 770,
            WINDOW_YSIZE = 570,
            MAX_XTILE = 49,
            MAX_YTILE = 49;
        #endregion
        public const string
        #region Values
            WINDOW_TITLE = "Game",
            DEBUG_WINDOW_TITLE = "Debug",
            SPRITE_FOLDER = "Sprites/",
            DEFAULT_SPRITE_EXT = ".png";
        #endregion
        #endregion

        //-Tiles-//
        public class Tile
        {
            public XY LOCATION;
            public Rectangle DIMENSION;
            public Bitmap FORE_TEXTURE;
            public Bitmap BACK_TEXTURE;
            public bool COLLSION = false;
            public const int
                XSIZE = 30,
                YSIZE = 30;
            public void SetLocation(int x, int y)
            {
                LOCATION = new XY(x, y);
                DIMENSION = new Rectangle(LOCATION.X * 30, LOCATION.Y * 30, XSIZE, YSIZE);
            }
        }

        //-Methods-//
        public class Init
        {
            public void _Player()
            {
                //must be run only at first load. If runned twice, it will reset the location.
                Player.LOCATION = new XY(Player.DEFAULT_XLOC, Player.DEFAULT_YLOC);
                Player.DIMENSION = new Rectangle(Player.LOCATION.X, Player.LOCATION.Y, Player.DEFAULT_XSIZE, Player.DEFAULT_YSIZE);
                Player.TEXTURE = new Bitmap(Player.DEFAULT_SPRITE);
            }
            public void _Window(Form window)
            {
                window.Size = new Size(WINDOW_XSIZE, WINDOW_YSIZE);
                window.Text = WINDOW_TITLE;
                window.BackColor = Color.Black;
                window.Controls.Add(Members.VIEW);
                Members.VIEW.BringToFront();
            }

//ERROR - ERROR - ERROR - ERROR//
            public void _SetTiles(string mapFile)
            {
                int Y = 0, X = 0;
                string[] Parameter = new string[4];
                string[] TilesInfo = File.ReadAllLines(mapFile);
                for (int y = 0; y < TilesInfo.Count(); y++)
                {
                    string[] WordsInfo = TilesInfo[y].Split(' ');
                    Y = TilesInfo.Count();
                    for (int x = 0; x < WordsInfo.Count(); x++)
                    {
                        X = WordsInfo.Count();
                        Parameter = WordsInfo[x].Split(':');
                    }
                }
                Members.T_XCOUNT = X;
                Members.T_YCOUNT = Y;
                Members.WORLD = new Panel { Location = new Point(0, 0), Size = new Size(X * 30, Y * 30), Visible = true, BackColor = Color.Gray };
                Members.VIEW.Controls.Add(Members.WORLD);
                Members.WORLD.BringToFront();
                for (int y = 0; y < Y; y++)
                {
                    for (int x = 0; x < X; x++)
                    {
                        Members.TILE[x, y].FORE_TEXTURE = new Bitmap(SPRITE_FOLDER + Parameter[0] + DEFAULT_SPRITE_EXT); //ERROR IS HERE <<<<<<<<<<<<<<
                        Members.TILE[x, y].BACK_TEXTURE = new Bitmap(SPRITE_FOLDER + Parameter[1] + DEFAULT_SPRITE_EXT);
                        if (Parameter[3] == "06")
                            Members.TILE[x, y].COLLSION = true;
                        else
                            Members.TILE[x, y].COLLSION = false;
                    }
                }
            }
            public void _StartEngine()
            {
                Members.WORLD.Paint += new PaintEventHandler(_OnRender);
            }
            public void _OnRender(object sender, PaintEventArgs e)
            {
                if (Members.FIRST_TIME_ENGINE_INIT == false)
                {
                    Members.ENGINE.Init(e.Graphics);
                    Members.ENGINE.StartRender();
                    Members.FIRST_TIME_ENGINE_INIT = true;
                }
                else
                {
                    if (Members.PLAYER_RENDER == true)
                    {
                        Members.ENGINE.RenderPlayer();
                    }
                    else
                    {
                    }
                }

            }

        }
        public static void Render(bool RenderPlayer) //SetTile must have been run before this method, or else it'll throw an Execption.
        {
            if (RenderPlayer == true)
            {
                Members.WORLD.Paint += new PaintEventHandler(Render);
                void Render(object sender, PaintEventArgs e)
                {

                }
            }
            else
            {
                Members.WORLD.Paint += new PaintEventHandler(Render);
                void Render(object sender, PaintEventArgs e)
                {

                }
            }

        }
    }

}


What I have tried:

I have tried to look if it was going above the index, but the exception shows up when
TILES[0,0] = new Bitmap(...)
is called...
Posted
Updated 4-Jun-18 15:43pm
v2

1 solution

This is not in your code:
C#
TILES[0,0] = new Bitmap(...)

Quote:
How do I fix a "system.indexoutofrangeexception" in my 2D game?

Somewhere, you try to access an array element than does not exist. this mean that you try to access element 15 in an array of size 10.
Advice: Since we can't do it for you, run your code under debugger and when you reach the error, inspect the arrays and variables where the error is, there is inconsistent value.

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900