From 70ae98a4c7a44b279608f482f13f3af7b7705fe2 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 28 Oct 2014 13:19:08 +0100 Subject: [PATCH] Verhindern von OutOfBounds bei dem Keyboard. --- .../spacescooter/control/Keyboard.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/de/teamteamteam/spacescooter/control/Keyboard.java b/src/de/teamteamteam/spacescooter/control/Keyboard.java index 52c2f9c..d9ff803 100644 --- a/src/de/teamteamteam/spacescooter/control/Keyboard.java +++ b/src/de/teamteamteam/spacescooter/control/Keyboard.java @@ -2,25 +2,26 @@ package de.teamteamteam.spacescooter.control; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; +import java.util.ArrayList; public class Keyboard implements KeyListener { - private static boolean[] keys = new boolean[150]; //TODO: This might kill the game in case there are keycodes > 150 + private static ArrayList activeKeys = new ArrayList(); public static boolean isKeyDown(int keyCode) { - if (keyCode >= 0 && keyCode < keys.length) { - return keys[keyCode]; - } - return false; + return Keyboard.activeKeys.contains((Integer) keyCode); } public void keyPressed(KeyEvent e) { - keys[e.getKeyCode()] = true; + if(Keyboard.activeKeys.contains((Integer) e.getKeyCode())) return; + Keyboard.activeKeys.add((Integer) e.getKeyCode()); } public void keyReleased(KeyEvent e) { - keys[e.getKeyCode()] = false; + if(Keyboard.activeKeys.contains((Integer) e.getKeyCode())) { + Keyboard.activeKeys.remove((Integer) e.getKeyCode()); + } } public void keyTyped(KeyEvent e) {