Hey all
I have an odd one here regarding GameStates and the GameControlManager.
In my test app, I have two GameStates - a menu and the ingame game state. To switch between the two, I use the Escape key. The initInput method for my menu game state looks like this …
controlManager = new GameControlManager();
GameControl esc = controlManager.addControl("esc");
esc.addBinding(new KeyboardBinding(KeyInput.KEY_ESCAPE));
ActionController gamemenuesc = new ActionController(esc, new GameControlAction() {
public void pressed(GameControl control, float time) {
System.out.println("pressed");
gameGameState.setActive(true);
gameMenuGameState.setActive(false);
}
public void released(GameControl control, float time) {
System.out.println("released");
}
});
this.getRootNode().addController(gamemenuesc);
... and the initInput method for my ingame game state looks like this ...
input = new FirstPersonHandler(cam, 10, 1);
controlManager = new GameControlManager();
GameControl esc = controlManager.addControl("esc");
esc.addBinding(new KeyboardBinding(KeyInput.KEY_ESCAPE));
ActionController ingameesc = new ActionController(esc, new GameControlAction() {
public void pressed(GameControl control, float time) {
System.out.println("pressed");
gameMenuGameState.setActive(true);
gameGameState.setActive(false);
}
public void released(GameControl control, float time) {
System.out.println("released");
}
});
this.getRootNode().addController(ingameesc);
I start out in my ingame game state. When I first press the Escape key, nothing happens. I see the "pressed" and "released" system outs, but no change in active game states. Then if I press Escape again, it works and I find myself in the menu game state. Then, if I press Escape from my menu game state, I get back into my ingame game state, but the screen does not refresh until I move the mouse (or issue some other FirstPersonHandler control).
The really strange thing is that once I have been through these two issues, the Escape key works as intended, swapping me between game states without any issue at all.
Any idea why my first run through these game controls might be showing these issues, and then working after that ?
Thanks all
Mak
Edit: Definately something to do with the Escape key triggering events on both game states the first time through. I can see that the "pressed" logic is running for the ingame game state and then immediately after (for the same key press) the "pressed" logic for the menu game state is running. That is why we are dropping back into the game game state. However, this doesn't explain why it only happens the first time through (I expect there's some sort of race conditions at play here) and also how a key pressed can be triggered after the key has been pressed in another game state.