이 예외는 게임 설정을 4 LengthSides
에서 9/16/25로 변경하는 경우에만 발생합니다. 미리 감사드립니다.퍼즐 게임 인덱스가 범위를 벗어남 예외
int LengthSides = Properties.Settings.Default.NumberOfSquares;
Image image;
PictureBox picBoxWhole = null;
PictureBox[] picBoxes = null;
Image[] images = null;
private void playLevel()
{
try
{
currentLevel = LengthSides;
if (picBoxWhole != null)
{
panel1.Controls.Remove(picBoxWhole);
picBoxWhole.Dispose();
picBoxWhole = null;
}
if (picBoxes == null)
{
picBoxes = new PictureBox[currentLevel];
images = new Image[currentLevel];
}
int numRow = (int)Math.Sqrt(currentLevel);
int numCol = numRow;
int unitX = panel1.Width/numRow;
int unitY = panel1.Height/numCol;
int[] indice = new int[currentLevel];
for (int i = 0; i < currentLevel; i++)
{
indice[i] = i;
if (picBoxes[i] == null)
{
picBoxes[i] = new MyPictureBox();
//picBoxes[i].BorderStyle = BorderStyle.Fixed3D;
picBoxes[i].Click += OnPuzzleClick;
picBoxes[i].MouseHover += PB_MouseHover;
picBoxes[i].MouseLeave += PB_MouseLeave;
picBoxes[i].Refresh();
}
picBoxes[i].Width = unitX;
picBoxes[i].Height = unitY;
((MyPictureBox)picBoxes[i]).Index = i;
createBitmapImage(image, images, i, numRow, numCol, unitX, unitY);
picBoxes[i].Location = new Point(unitX * (i % numCol), unitY * (i/numCol));
if (!panel1.Controls.Contains(picBoxes[i]))
{
panel1.Controls.Add(picBoxes[i]);
}
}
shuffle(ref indice);
for (int i = 0; i < currentLevel; i++)
{
picBoxes[i].Image = images[indice[i]];
((MyPictureBox)picBoxes[i]).ImageIndex = indice[i];
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
업데이트 : 테스트하고이 버그를 해결하기 위해이 프로젝트를 공유 할 수 있습니다 . 큰 값에서 작은 값으로 변경하는 경우 (예 : 25 - 4), 그렇지 않으면이 예외가 발생합니다.
private void switchImages(MyPictureBox box1, MyPictureBox box2)
{
int tmp = box2.ImageIndex;
box2.Image = images[box1.ImageIndex];
box2.ImageIndex = box1.ImageIndex;
box1.Image = images[tmp];
box1.ImageIndex = tmp;
if (isSuccessful())
{
stopAllTimers();
firstBox.BorderStyle = BorderStyle.None;
secondBox.BorderStyle = BorderStyle.None;
box1.BorderStyle = BorderStyle.None;
box2.BorderStyle = BorderStyle.None;
Properties.Settings.Default.LevelCompleted = true;
}
}
붙여주세요 – Pikoh
가 적어주십시오 전체 예외 스택 추적하는에 당신이'LengthSides'을 변경할 때 – BugFinder
당신이 당신의'picBoxes'와'images' 배열을 초기화하는 오류 라인? 왜냐하면 두 번째로'playLevel'을 호출 할 때'picBoxes'는 null이 아니며 이미 초기화되어 있기 때문입니다. 나는 당신의 코드에서'LengthSides'를 바꾼 곳에서'picBoxes'를 null로 설정해야한다고 생각합니다. – Pikoh