如何修复“system.indexoutofrangeexception”?在我的2D游戏中? [英] How do I fix a "system.indexoutofrangeexception" in my 2D game?

查看:108
本文介绍了如何修复“system.indexoutofrangeexception”?在我的2D游戏中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



所以我正在编写游戏并加载图块,代码将读取地图信息所在的.map文件。但是,它给了我这个错误。



.map文件看起来像这样:

这只是一行...



 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 







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

命名空间核心
{
公共类核心
{
// -----------核心----- ------- //
// - 结构 - //
公共结构XY
{
public int
X,
Y;
public XY(int x,int y)
{
X = x; Y = y;
}
}

// - 静态成员 - //
公共类成员
{
公共静态面板视图=新面板
{
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;

}

// - 播放器 - //
公共类播放器
{
公共静态位图TEXTURE;
public static Rectangle DIMENSION;
公共静态XY位置;
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);
}
其他
{
TEXTURE =新位图(DEFAULT_SPRITE);
}
返回true;
}
}

// - 常量值 - //
#region Const
public const int
#region值
WINDOW_XSIZE = 770,
WINDOW_YSIZE = 570,
MAX_XTILE = 49,
MAX_YTILE = 49;
#endregion
公共const字符串
#region值
WINDOW_TITLE =游戏,
DEBUG_WINDOW_TITLE =调试,
SPRITE_FOLDER =Sprites / ,
DEFAULT_SPRITE_EXT =。png;
#endregion
#endregion

// - Tiles - //
public class Tile
{
public XY LOCATION;
public Rectangle DIMENSION;
公共位图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 =新矩形(LOCATION.X * 30,LOCATION.Y * 30,XSIZE,YSIZE);
}
}

// - 方法 - //
公共类Init
{
public void _Player()
{
//必须仅在首次加载时运行。如果运行两次,它将重置位置。
Player.LOCATION =新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 =新位图(Player.DEFAULT_SPRITE);
}
public void _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();
}

//错误 - 错误 - 错误 - 错误//
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();
参数= 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 =新位图(SPRITE_FOLDER +参数[0] + DEFAULT_SPRITE_EXT); //错误在这里<<<<<<<<<<<<<<<
Members.TILE [x,y] .BACK_TEXTURE =新位图(SPRITE_FOLDER +参数[1] + DEFAULT_SPRITE_EXT);
if(参数[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。初始化(e.Graphics);
Members.ENGINE.StartRender();
Members.FIRST_TIME_ENGINE_INIT = true;
}
else
{
if(Members.PLAYER_RENDER == true)
{
Members.ENGINE.RenderPlayer();
}
其他
{
}
}

}

}
public static void Render(bool RenderPlayer)//必须在此方法之前运行SetTile,否则它将抛出Execption。
{
if(RenderPlayer == true)
{
Members.WORLD.Paint + = new PaintEventHandler(Render);
void Render(object sender,PaintEventArgs e)
{

}
}
else
{
Members.WORLD。 Paint + = new PaintEventHandler(Render);
void渲染(对象发送者,PaintEventArgs e)
{

}
}

}
}

}





我的尝试:



我试图查看它是否高于索引,但是当

 TILES [0,0] =新位图(...)被称为... 

解决方案

这不在你的代码中:

 TILES [< span class =code-digit> 0 , 0 ] =  new 位图(。 ..)



Quote:

如何修复system.indexoutofrangeexception在我的2D游戏中?



某处,你试图访问一个不存在的数组元素。这意味着您尝试访问大小为10的数组中的元素15.

建议:由于我们无法为您执行此操作,请在调试器下运行您的代码,当您遇到错误时,请检查错误所在的数组和变量,值不一致。



您的代码行为不符合您的预期,您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里显示你的代码是什么正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它不知道你应该做什么,它没有发现bug,它只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

在Visual Studio中调试C#代码 - YouTube [ ^ ]

调试器只显示你的代码正在做,你的任务是与它应该做的事情进行比较。


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...

解决方案

This is not in your code:

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.


这篇关于如何修复“system.indexoutofrangeexception”?在我的2D游戏中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆