2015-03-30 2 views
1

Accordin к экрану отладки ошибки в:Почему я получаю исключение Array index out of bounds?

1.Line 16: (Класс RandomLevel)

protected void generateLevel() { 
    for (int y = 0; y < height; y++) { 
     for (int x = 0; y < width; x++) { 
      tiles[x + y * width] = random.nextInt(4); //Here is the error. 
     } 
    } 
} 

2.Line 15: (Уровень Класс)

public Level(int width, int height) { 
    this.width = width; 
    this.height = height; 
    tiles = new int[width * height]; 
    generateLevel();        //Here is the error. 
} 

3. Строка 10: (Класс RandomLevel)

public RandomLevel(int width, int height) { 
    super(width, height); // Here is the error. 
} 

4. линия 43: (Класс Game)

public Game() { 
    Dimension size = new Dimension(width * scale, height * scale); 
    setPreferredSize(size); 

    screen = new Screen(width, height); 
    frame = new JFrame(); 
    key = new Keyboard(); 
    level = new RandomLevel(64, 64);     // Here is the error. 

    addKeyListener(key); 
} 

5.Line 124: (класс Game)

public static void main(String[] args) { 
    Game game = new Game();       // Here is the error. 
    game.frame.setResizable(false); 
    game.frame.setTitle(game.title); 
    game.frame.add(game); 
    game.frame.pack(); 
    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    game.frame.setLocationRelativeTo(null); 
    game.frame.setVisible(true); 

    game.start(); 
} 

Что я должен делать? Я понимаю, что такое исключение, но я не знаю, почему оно появляется. Помогите?

+0

детально это условие 'для (INT х = 0; y <ширина; x ++) ', копирование может иногда вызвать проблемы –

ответ

7

Неправильное состояние вашего внутреннего цикла for.

for (int x = 0; y < width; x++) { 

Вы зацикливание над x, но ваше состояние включает в себя y снова. Попробуйте

for (int x = 0; x < width; x++) { 
+0

Благодаря вам, теперь я знаю im retard. Благодаря! – SvelterEagle

1

Вы

for (int x = 0; y < width; x++) { 

вы намерены

for (int x = 0; x < width; x++) { 
1

у вас есть две ошибки:

1)

for (int x = 0; y < width; x++) { 

изменения у к х

2)

tiles = new int[width * height]; 

но

tiles[x + y * width] = random.nextInt(4); //Here is the error. 

это будет продолжаться до

tiles[width+height*width] 

, что приведет к ошибке, изменить

tiles = new int[width * height]; 

к

tiles = new int[width + width * height]; 
Смежные вопросы