From 12cb4fd10f844620811cd191d21d96e30fddf98d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bokros=20B=C3=A1lint?= Date: Fri, 13 May 2016 21:31:46 +0200 Subject: [PATCH] Fixed multiple key handlers being registered on restarted game --- cicaprojekt/ApplicationFrame.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cicaprojekt/ApplicationFrame.java b/cicaprojekt/ApplicationFrame.java index 1ee98bb..f799577 100644 --- a/cicaprojekt/ApplicationFrame.java +++ b/cicaprojekt/ApplicationFrame.java @@ -3,6 +3,7 @@ package cicaprojekt; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import java.awt.*; +import java.awt.event.KeyListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; @@ -17,6 +18,8 @@ public class ApplicationFrame implements Runnable private JList filelist; + private KeyListener keyHandler = new Control.KeyHandler(); + private class ListMouseHandler extends MouseAdapter{ @Override public void mouseClicked(MouseEvent e) { @@ -47,6 +50,7 @@ public class ApplicationFrame implements Runnable public void backToMapSelection() { jframe.remove(gamepanel); + jframe.removeKeyListener(keyHandler); initMapselector(); jframe.remove(mapselectorpanel); jframe.add(mapselectorpanel); @@ -81,7 +85,7 @@ public class ApplicationFrame implements Runnable private void setGameParams(File dungeonFile) throws IOException { gamepanel = new Display(); - jframe.addKeyListener(new Control.KeyHandler()); + jframe.addKeyListener(keyHandler); Game.instance.setDisplay(gamepanel); Game.instance.setDungeon(new Dungeon()); Game.instance.startGame(dungeonFile);