Created
July 31, 2015 15:02
-
-
Save hoenn/1ba7597fa4436f5b18a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.event.KeyEvent; | |
import java.awt.event.KeyListener; | |
public class InputHandler implements KeyListener { | |
private Character lastKey; | |
//This current set up only registers keyPressed | |
public InputHandler(Game game) | |
{ | |
game.addKeyListener(this); | |
lastKey=null; | |
} | |
@Override | |
public void keyPressed(KeyEvent e) | |
{ | |
if((lastKey==null || lastKey!= e.getKeyChar())) | |
{ | |
lastKey=e.getKeyChar(); | |
applyInput(e.getKeyCode()); | |
} | |
} | |
@Override | |
public void keyReleased(KeyEvent e) | |
{ | |
lastKey=null; | |
} | |
@Override | |
public void keyTyped(KeyEvent e) | |
{ | |
// TODO Auto-generated method stub | |
} | |
public void applyInput(int keyCode) | |
{ | |
//This is where the input is actually handled | |
//Ex.1 | |
//Create function calls to handle gameplay here | |
//abstract "gameplay functions" to a seperate class and | |
//call that here | |
//Ex.2 | |
//Create a data structure to represent the keys pressed | |
//then view and call functions from another part of the code | |
if(KeyEvent.VK_LEFT==keyCode) | |
{ | |
//Ex.1 | |
//controller.moveCharacter(-1); | |
//Ex.2 | |
//keys[0]=1; | |
} | |
if(KeyEvent.VK_RIGHT==keyCode) | |
{ | |
} | |
if(KeyEvent.VK_UP==keyCode) | |
{ | |
} | |
if(KeyEvent.VK_DOWN==keyCode) | |
{ | |
} | |
if(KeyEvent.VK_ENTER==keyCode) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment