Original Post
I'm having trouble deciding which is the best way to go with this. My game has a few different interfaces that the player interacts with. The normal moving around the world interface, the shopping interface, and the battle interface. Currently, my main applet class is set as the KeyListener. One way I could handle the different input for the various states is to just have a big switch statement in the key events for the applet's class that checks a state flag and processes the input there. Or, I could set up an object for each interface and make them KeyListeners that are added and removed, depending on what the player is doing. Or, I could still just use the applet's class as the keylistener, check the state flag and then manually call the KeyPressed/KeyReleased methods from within those methods in the main class. In other words: Option 1: (within my applet class) Option 2: I'd have a few classes classes (MovementKeyListener, BattleKeyListener, ShoppingKeyListener, etc), each one interfacing KeyListener. When the player enters a battle, for example, the MovementKeyListener object would be removed and the BattleKeyListener would be added. Option 3: Like option #2, but without adding/removing any KeyListeners. Assuming you can make any sense of what I tried to say there, which would be the better way to go (if any)? Or does it really matter? I'd prefer not to have all of the key events handled within the main class just for organization's sake. So I'm thinking either option 2 or 3.
public void KeyPressed (KeyEvent e) {
switch (state) {
case STATE_MOVEMENT:
// process key event
case STATE_BATTLE:
// process key event
...
}
}
public void KeyPressed (KeyEvent e) {
switch (state) {
case STATE_MOVEMENT:
MovementKeyListener.KeyPressed(e);
case STATE_BATTLE:
BattleKeyListener.KeyPressed(e);
...
}
}