2017-05-22 20 views
-3

이 예외는 게임 설정을 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); 
      } 
     } 

오류 스크린 샷 : enter image description here

업데이트 : 테스트하고이 버그를 해결하기 위해이 프로젝트를 공유 할 수 있습니다 . 큰 값에서 작은 값으로 변경하는 경우 (예 : 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; 
      } 
     } 

화면 : enter image description here

+0

붙여주세요 – Pikoh

+0

가 적어주십시오 전체 예외 스택 추적하는에 당신이'LengthSides'을 변경할 때 – BugFinder

+0

당신이 당신의'picBoxes'와'images' 배열을 초기화하는 오류 라인? 왜냐하면 두 번째로'playLevel'을 호출 할 때'picBoxes'는 null이 아니며 이미 초기화되어 있기 때문입니다. 나는 당신의 코드에서'LengthSides'를 바꾼 곳에서'picBoxes'를 null로 설정해야한다고 생각합니다. – Pikoh

답변

0

나는 그것을 알아 냈습니다. picBoxes = new MyPictureBox[currentLevel]; images = new Image[currentLevel]; 초기화 전에 모두 0으로 초기화했으며 PictureBox[] 클래스에서 MyPictureBox[] picBoxes = null;으로 변경되었으며 이제 작동합니다.

  if (picBoxes != null) 
      { 
       for (int i = 0; i < picBoxes.Length; i++) 
       { 
        if (picBoxes[i].Image != null) 
        { 
         picBoxes[i].Image = null; 
        } 

        picBoxes[i].Index = 0; 
        picBoxes[i].ImageIndex = 0; 
        picBoxes[i].Width = 0; 
        picBoxes[i].Height = 0; 
        picBoxes[i] = null; 
       } 

       images = null; 
      } 
0

변경하는 경우 LengthSides 당신이 배열 picBoxesimages의 크기를 변경해야합니다.

이제 이전 크기

 if (picBoxes == null) 
     { 
      picBoxes = new PictureBox[currentLevel]; 
      images = new Image[currentLevel]; 
     }` 

내가 제대로 질문을 이해 희망을 사용하고 있습니다.

+0

배열의 크기를 변경하면 무엇을 의미합니까? 여기서 변경되지 않습니다 :'currentLevel = LengthSides;'? 감사. – Cobra91151

0

귀하의 문제는 picBoxes 그렇게 null 경우 분기 초기화에서

if (picBoxes == null) 
{ 
    picBoxes = new PictureBox[currentLevel]; 
    images = new Image[currentLevel]; 
} 

이이 블록이 실행되고 있다는 점이다. 변수 picBoxesimages는 다음 질문에

currentLevel = LengthSides; 

에 의해 정의된다 currentLevel의 값에 따라 폭으로 설정되어, 당신은 문제가 4 LengthSides에서 때 "변화하는 게임 설정을 발생 말했다 9/16/25 ".

이 메서드는 각 레벨에서 호출되는 playLevel() 메서드에서 알아 냈습니다. 그러나 크기가 다음 4에서 9

currentLevel = LengthSides; // becomes 9 

를 업데이트되는 경우 그래서, 당신은 picBoxes으로 아무것도하지 않습니다. 따라서 if 문 아래의 블록은

if (picBoxes == null) 
{ 
    picBoxes = new PictureBox[currentLevel]; 
    images = new Image[currentLevel]; 
} 

으로 표시되지 않습니다. 따라서 picBoxes은 여전히 ​​오래되었지만 크기는 작습니다. 4. 9이 아닙니다. currentLevel이 더 높은 경우 물론 이것은 범위를 벗어난 오류를 발생시킵니다. picBoxes의 길이는 지금까지 최대 4 인 반면에 0에서 8 (i < currentLevel)까지 반복하려고합니다.

간단한 해결책 : picBoxesimages 필드를 다시 초기화하십시오. (그냥 상태 검사를 제거하십시오)

+0

'LengthSides' 값을 설정 한 후 조건을 제거하거나'picBoxes = null'을 설정하면'picBoxes'가 전환되지 않습니다 (이미지를 조각으로 나눕니다). – Cobra91151