# Game Crash



## HiImSkulte (22. Nov 2016)

Ich benutze LWJGL 2.9.3 und Slick. JavaSE-1.8.
Bin gerade dabei ein 2D Tower Defense Game zu programmieren und in meiner Enemy.class habe ich folgenden Code, mein Spiel crasht allerdings nachdem es einen Blackscreen kriegt. Bitte um Hilfe, danke 


```
package data;

import org.newdawn.slick.opengl.Texture;

import static helpers.Artist.*;
import static helpers.Clock.*;

import java.util.ArrayList;

public class Enemy {

   private int width, height, health, currentCheckpoint;
   private float speed, x, y;
   private Texture texture;
   private Tile startTile;
   private boolean first = true;
   private TileGrid grid;

   private ArrayList<Checkpoint> checkpoints;
   private int[] directions;

   public Enemy(Texture texture, Tile startTile, TileGrid grid, int width, int height, float speed) {
     this.texture = texture;
     this.startTile = startTile;
     this.x = startTile.getX();
     this.y = startTile.getY();
     this.width = width;
     this.height = height;
     this.speed = speed;
     this.grid = grid;
     this.checkpoints = new ArrayList<Checkpoint>();
     this.directions = new int[2];
     // X DIRECTION
     this.directions[0] = 0;
     // Y DIRECTION
     this.directions[1] = 0;
     directions = FindNextD(startTile);
     this.currentCheckpoint = 0;
     PopulateCheckpointList();
   }

   public void Update() {
     if (first)
       first = false;
     else {
       if (CheckpointReached()) {
         currentCheckpoint++;
       } else {
         x += Delta() * checkpoints.get(currentCheckpoint).getxDirection();
         y += Delta() * checkpoints.get(currentCheckpoint).getyDirection();
       }
     }
   }

   private boolean CheckpointReached() {
     boolean reached = false;
     Tile t = checkpoints.get(currentCheckpoint).getTile();
     // CHECK IF POSITION REACHED TILE WITHIN VARIANCE OF 3 (ARBITRARY)
     if (x > t.getX() - 3 && x < t.getX() + 3 &&
       y > t.getY() - 3 && y < t.getY() + 3) {
       reached = true;
       x = t.getX();
       y = t.getY();
     }
     return reached;
   }

   private void PopulateCheckpointList() {
     checkpoints.add(FindNextC(startTile, directions = FindNextD(startTile)));
     int counter = 0;
     boolean cont = true;
     while (cont) {
       int[] currentD = FindNextD(checkpoints.get(counter).getTile());
       // CHECK IF A NEXT DIRECTION/CHECKPOINT EXISTS, END AFTeR 20 CHECKPOINTS (ARBITRARY)
       if (currentD[0] == 2 || counter == 20) {
         cont = false;
       } else {
         checkpoints.add(FindNextC(checkpoints.get(counter).getTile(),
         directions = FindNextD(checkpoints.get(counter).getTile())));
       }
       counter++;
     }
   }

   private Checkpoint FindNextC(Tile s, int[] dir) {
     Tile next = null;
     Checkpoint c = null;
     //BOOLEAN TO DECIDE IF NEXT CHECKPOINT IS FOUND
     boolean found = false;
     //INTEGER TO INCREMENT EACH LOOP
     int counter = 1;
     while (!found) {
       if (s.getType() != grid.GetTile(s.getXPlace() + dir[0 * counter], s.getYPlace() + dir[1] * counter).getType()) {
         found = true;
         //MOVE COUNTER BACK 1 TO FIND TILE BEFORE NEW TILETYPE
         counter -= 1;
         next = grid.GetTile(s.getXPlace() + dir[0 * counter],
         s.getYPlace() + dir[1] * counter);
       }
       counter++;
     }
     c = new Checkpoint(next, dir[0], dir[1]);
     return c;
   }
   
   private int[] FindNextD(Tile s) {
     int[] dir = new int[2];
     Tile u = grid.GetTile(s.getXPlace(), s.getYPlace() - 1);
     Tile r = grid.GetTile(s.getXPlace() + 1, s.getYPlace());
     Tile d = grid.GetTile(s.getXPlace(), s.getYPlace() + 1);
     Tile l = grid.GetTile(s.getXPlace() - 1, s.getYPlace());
     if (s.getType() == u.getType()) {
       dir[0] = 0;
       dir[1] = -1;
     } else if (s.getType() == r.getType()) {
       dir[0] = 1;
       dir[1] = 0;
     } else if (s.getType() == d.getType()) {
       dir[0] = 0;
       dir[1] = 1;
     } else if (s.getType() == l.getType()) {
       dir[0] = -1;
       dir[1] = 0;
     } else {
       dir[0] = 2;
       dir[1] = 2;
       System.out.println("NO DIRECTION FOUND");
     }

     return dir;
   }

   /*
   private boolean pathContinues() {
   boolean answer = true;
   Tile myTile = grid.GetTile((int) (x / 64), (int) (y / 64));
   Tile nextTile = grid.GetTile((int) (x / 64) + 1, (int) (y / 64));
   if (myTile.getType() != nextTile.getType())
   answer = false;
   return answer;
   }
   */

   public void Draw() {
     DrawQuadTex(texture, x, y, width, height);
   }

   public int getWidth() {
     return width;
   }

   public void setWidth(int width) {
     this.width = width;
   }

   public int getHeight() {
     return height;
   }

   public void setHeight(int height) {
     this.height = height;
   }

   public int getHealth() {
     return health;
   }

   public void setHealth(int health) {
     this.health = health;
   }

   public float getSpeed() {
     return speed;
   }

   public void setSpeed(float speed) {
     this.speed = speed;
   }

   public float getX() {
     return x;
   }

   public void setX(float x) {
     this.x = x;
   }

   public float getY() {
     return y;
   }

   public void setY(float y) {
     this.y = y;
   }

   public Texture getTexture() {
     return texture;
   }

   public void setTexture(Texture texture) {
     this.texture = texture;
   }

   public Tile getStartTile() {
     return startTile;
   }

   public void setStartTile(Tile startTile) {
     this.startTile = startTile;
   }

   public boolean isFirst() {
     return first;
   }

   public void setFirst(boolean first) {
     this.first = first;
   }

   public TileGrid getTileGrid() {
     return grid;
   }
}
```


----------



## Robat (22. Nov 2016)

1. Benutze bitte die Code-Tags. (Einfügen-->Code-->Java)
2. Poste bitte gleichzeitig deine Fehlermeldung, die du bekommst


----------



## HiImSkulte (23. Nov 2016)

Das Problem ist dass in der Konsole kein Fehler ausgegeben wird.


----------



## HiImSkulte (23. Nov 2016)

*Ok, hier nochmal:
Habe jetzt folgenden Fehlercode:*
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at data.TileGrid.GetTile(TileGrid.java:43)
    at data.Enemy.FindNextC(Enemy.java:103)
    at data.Enemy.PopulateCheckpointList(Enemy.java:74)
    at data.Enemy.<init>(Enemy.java:41)
    at data.Boot.<init>(Boot.java:42)
    at data.Boot.main(Boot.java:61)

Hier mein Boot, Enemy und TileGrid Code:

*Boot:*


```
package data;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.opengl.Texture;

import helpers.Clock;

import static org.lwjgl.opengl.GL11.*;

import static helpers.Artist.*;

public class Boot {
  
    public Boot() {
      
        BeginSession();
          
        int[][] map = {
              
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

        };
      
        TileGrid grid = new TileGrid(map);
        grid.SetTile(3, 4, grid.GetTile(5, 7).getType());
        Enemy e = new Enemy(QuickLoad("enemy64"), grid.GetTile(10, 8), grid, 64, 64, 6);
        Wave wave = new Wave(20, e);
        Player player = new Player(grid);
      
        while(!Display.isCloseRequested()) {
            Clock.update();
          
            grid.Draw();
            wave.Update();
            player.Update();
          
            Display.update();
            Display.sync(60);
        }
      
        Display.destroy();
    }
  
    public static void main(String[] args) {
        new Boot();
    }
  
}
```

*Enemy:
*

```
package data;

import org.newdawn.slick.opengl.Texture;

import static helpers.Artist.*;
import static helpers.Clock.*;
import static data.TileGrid.*;

import java.util.ArrayList;

public class Enemy {

    private int width, height, health, currentCheckpoint;
    private float speed, x, y;
    private Texture texture;
    private Tile startTile;
    private boolean first = true;
    private TileGrid grid;
  
    private ArrayList<Checkpoint> checkpoints;
    private int[] directions;
  
    public Enemy(Texture texture, Tile startTile, TileGrid grid, int width, int height, float speed) {
        this.texture = texture;
        this.startTile = startTile;
        this.x = startTile.getX();
        this.y = startTile.getY();
        this.width = width;
        this.height = height;
        this.speed = speed;
        this.grid = grid;
      
        this.checkpoints = new ArrayList<Checkpoint>();
        this.directions = new int[2];
        // X DIRECTION
        this.directions[0] = 0;
        // Y DIRECTION
        this.directions[1] = 0;
        directions = FindNextD(startTile);
        this.currentCheckpoint = 0;
        PopulateCheckpointList();
    }
  
    public void Update() {
        if (first)
            first = false;
        else {
            if (CheckpointReached()) {
                currentCheckpoint++;
            } else {
                x += Delta() * checkpoints.get(currentCheckpoint).getxDirection();
                y += Delta() * checkpoints.get(currentCheckpoint).getyDirection();
            }
        }
    }
  
    private boolean CheckpointReached() {
        boolean reached = false;
        Tile t = checkpoints.get(currentCheckpoint).getTile();
        if (x > t.getX() - 3 &&
                x < t.getX() + 3 &&
                y > t.getY() - 3 &&
                y < t.getY() + 3) {
          
            reached = true;
            x = t.getX();
            y = t.getY();
          
        }
        return reached;
    }
  
    private void PopulateCheckpointList() {
        checkpoints.add(FindNextC(startTile, directions = FindNextD(startTile)));
      
        int counter = 0;
        boolean cont = true;
        while (cont) {
            int[] currentD = FindNextD(checkpoints.get(counter).getTile());
            if (currentD[0] == 2 || counter == 20) {
                cont = false;
            } else {
                checkpoints.add(FindNextC(checkpoints.get(counter).getTile(),
                        directions = FindNextD(checkpoints.get(counter).getTile())));          
            }
            counter++;
        }
    }                                                                           
  
    private Checkpoint FindNextC(Tile s, int[] dir) {
        Tile next = null;
        Checkpoint c = null;
      
        //BOOLEAN TO DECIDE IF NEXT CHECKPOINT IS FOUND
        boolean found = false;
      
        //INTEGER TO INCREMENT EACH LOOP
        int counter = 1;
      
        while (!found) {
          
            if (s.getType() !=
                    grid.GetTile(s.getXPlace() + dir[0 * counter],
                            s.getYPlace() + dir[1] * counter).getType()) {
              
                found = true;
                //MOVE COUNTER BACK 1 TO FIND TILE BEFORE NEW TILETYPE
                counter -= 1;
                next = grid.GetTile(s.getXPlace() + dir[0 * counter],
                        s.getYPlace() + dir[1] * counter);
            }
              
            counter++;
        }
      
        c = new Checkpoint(next, dir[0], dir[1]);
        return c;
    }
      
    private int[] FindNextD(Tile s) {
        int[] dir = new int[2];
        Tile u = grid.GetTile(s.getXPlace(), s.getYPlace() - 1);
        Tile r = grid.GetTile(s.getXPlace() + 1, s.getYPlace());
        Tile d = grid.GetTile(s.getXPlace(), s.getYPlace() + 1);
        Tile l = grid.GetTile(s.getXPlace() - 1, s.getYPlace());
      
        if (s.getType() == u.getType()) {
            dir[0] = 0;
            dir[1] = -1;
        } else if (s.getType() == r.getType()) {
            dir[0] = 1;
            dir[1] = 0;
        } else if (s.getType() == d.getType()) {
            dir[0] = 0;
            dir[1] = 1;
        } else if (s.getType() == l.getType()) {
            dir[0] = -1;
            dir[1] = 0;
        }  else {
            System.out.println("NO DIRECTION FOUND");
        }
          
      
        return dir;
    }
  
    /*
    private boolean pathContinues() {
        boolean answer = true;
      
        Tile myTile = grid.GetTile((int) (x / 64), (int) (y / 64));
        Tile nextTile = grid.GetTile((int) (x / 64) + 1, (int) (y / 64));
      
        if (myTile.getType() != nextTile.getType())
            answer = false;
      
        return answer;
    }
    */
  
    public void Draw() {
        DrawQuadTex(texture, x, y, width, height);
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getHealth() {
        return health;
    }

    public void setHealth(int health) {
        this.health = health;
    }

    public float getSpeed() {
        return speed;
    }

    public void setSpeed(float speed) {
        this.speed = speed;
    }

    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getY() {
        return y;
    }

    public void setY(float y) {
        this.y = y;
    }

    public Texture getTexture() {
        return texture;
    }

    public void setTexture(Texture texture) {
        this.texture = texture;
    }

    public Tile getStartTile() {
        return startTile;
    }

    public void setStartTile(Tile startTile) {
        this.startTile = startTile;
    }

    public boolean isFirst() {
        return first;
    }

    public void setFirst(boolean first) {
        this.first = first;
    }
  
    public TileGrid getTileGrid() {
        return grid;
    }
  
  
  
}
```

*TileGrid:
*

```
package data;

import static helpers.Artist.*;

public class TileGrid {

    public Tile[][] map;
  
    public TileGrid() {
        map = new Tile[20][15];
        for (int i = 0; i < map.length; i++) {
            for(int j = 0; j < map[i].length; j++) {
                map[i][j] = new Tile(i * 64, j * 64, 64, 64, TileType.Grass);
            }
        }
    }
  
    public TileGrid(int[][] newMap) {
        map = new Tile[20][15];
        for(int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
  
                switch (newMap[j][i]) {
                case 0:
                    map[i][j] = new Tile(i * 64, j * 64, 64, 64, TileType.Grass);
                    break;
                case 1:
                    map[i][j] = new Tile(i * 64, j * 64, 64, 64, TileType.Dirt);
                    break;
                case 2:
                    map[i][j] = new Tile(i * 64, j * 64, 64, 64, TileType.Water);
                    break;
                }
            }
        }
    }
  
    public void SetTile(int xCoord, int yCoord, TileType type) {
        map[xCoord][yCoord] = new Tile(xCoord * 64, yCoord * 64, 64, 64, type);
    }
  
    public Tile GetTile(int xCoord, int yCoord) {
        return map[xCoord][yCoord];
    }
  
    public void Draw() {
        for (int i = 0; i < map.length; i ++) {
            for (int j = 0; j < map[i].length; j++) {
                map[i][j].Draw();            }
        }
    }
  
}
```


*Ich bin dankbar für jede Hilfe! Anscheinend macht die Methode GetTile Probleme, kann allerdings keines entdecken. Hoffe hier kann es jemand ^^

Danke!

MfG HiImSkulte 

(Im Anhang nochmal das ganze GameProject)*


----------



## JCODA (23. Nov 2016)

Du rufst *Tile u = grid.GetTile(s.getXPlace(), s.getYPlace()-1);* auf, und falls *s.getYPlace() == 0* ist, dann liefert dir 

*public Tile GetTile(int xCoord, int yCoord){
       return map[xCoord][yCoord];
 } *
eine arrayindexoutofbounds. 
Da solltest du zuvor testen, ob du innerhalb des arrays landest.


----------



## HiImSkulte (24. Nov 2016)

Sorry wenn die Frage dumm kommt aber, wie mach ich das denn? Bin noch relativ neu in Java. Schonmal danke!


----------



## Joose (24. Nov 2016)

Indem du überprüfst ob die übergebenen Parameter (xCoord bzw. yCoord) größer 0 sind und kleiner als die Arraylänge.



HiImSkulte hat gesagt.:


> ... wie mach ich das denn? Bin noch relativ neu in Java. Schonmal danke!


Deswegen würde ich dazu raten einfache kleine Sachen zu programmieren um die Grundlagen zu festigen. Erst dann sollte man sich an komplexere Sachen wie ein Spiel wagen


----------



## HiImSkulte (24. Nov 2016)

Hatte eigentlich die Idee nach einem Tutorial das zu machen und somit ein paar Sachen zu lernen und mitzunehmen aber auf einmal ging es nicht mehr bei mir obwohl ich nichts anderes gemacht habe. Aber ist wahrscheinlich besser, verstehe grade nicht wie das funktioniert. Habe xCoord und yCoord ja als integer übergeben und nicht explizit gesagt dass diese größer als 0 sind. Beide sind ja von der Map die so aussieht:


```
int[][] map = {   
               
                {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
                {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
                {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
                {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
                {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
                {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
               
        };
```

Diese sollen ja anzeigen wo der Gegner e sich gerade befindet.

Verstehe wie gesagt nicht so ganz was ich machen soll. Ich werde das Projekt erstmal zur Seite legen und anderes machen bis ich mehr Erfahrung habe und es weiter machen kann.

Danke für die Hilfe!

MfG HiImSkulte


----------

