From 347f8f5dba10f2a5bc9f058ad032eca2adc18f82 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Wed, 25 Dec 2019 19:11:33 +0400 Subject: [PATCH] Bounds check on getTile --- core/src/com/me/pacman/level/Level.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/core/src/com/me/pacman/level/Level.java b/core/src/com/me/pacman/level/Level.java index 77b2836..79d3d66 100644 --- a/core/src/com/me/pacman/level/Level.java +++ b/core/src/com/me/pacman/level/Level.java @@ -13,6 +13,9 @@ public class Level { // Grid of tiles, [rows][columns] public LevelTile[][] tiles; + public int width; + public int height; + public Level(PacDude game, String level) { this.game = game; @@ -22,10 +25,13 @@ public class Level { LevelLoader loader = new LevelLoader(level); tiles = loader.loadLevel(); + + height = tiles.length; + width = tiles[0].length; } public LevelTile getTile(int x, int y) { - return tiles[y][x]; + return y >= 0 && y < height && x >= 0 && x < width ? tiles[y][x] : null; } public LevelTile getTile(float x, float y) {