I have two BaseAppState applications. One launches the other when a key is pressed.
'ESC" key opens up a HelpMenu and the 1st app launches the HelpBaseApp.
THis causes the 1st app to be disabled. It remove listeners to keyevents.
2nd app opens and starts listening to keyevents. To quit this App you hit “ESC” same key.
When you do this, it disables the 2nd app and re enables the 1st app. Inside OnEnable it adds listener to key events. Which one of them is “ESC”/
It then immediate on the 1st app gets the “ESC” key called from the user hitting “ESC” on the 2nd app to leave the menu.
What am I doing wrong?
By “one launches the other”, do you mean launches a separate JVM?
No, bad wording. Just meaning one activates the other.
Basically both are activated on launch of game. The 2nd one is set to enabled = false, so it will not run. The “onEnabled” and “onDisabled” function of the apps adds/removes listeners to key events.
So 1st. on app opening, is set to active. when the “esc” key is pressed, it get the app class through statemanager and enabled it, and then disabled itself.
the 2nd app on being enabled through statemanger and calling setEnabled(true), on the onEnabled function starts the listeners for the events it cares about.
2nd app: one event is listen for the “ESC” to quit the app (this is a help menu). When it gets the 'onAction" for “Esc” it will setEnabled for itself to false and then through statemanger get 1st app class and setEnabled(true) to reactive it.
Then (Not sure 100% on JME ordering), 2nd app gets the “onDisabled” function called and it removes the key event listener for that app.
Statemanger: calls 1st app “onEnabled” because 2nd app set it to true. 1st. app then starts the listeners for inputmanager addListener(this, …).
1st app immediately gets an ‘onAction’ event for “esc” key, which was done in the 2nd app.
So at this point, only thing I’m thinking, is that the input manager is still processing through all the listeners on a different thread as the statemanager. So statemanager is enabling 1st app before input manager loops through everything and 1st. gets its listener turned on before input manager is done processing all listeners for “esc” (Guessing, since I didn’t this the key more than once).
So two different SimpleApplications in the same JVM?
Or are you just talking about app states?
I may have misunderstood this sentence and maybe “applications” is not Applications.
Two app states inside the same JVM.
I just do the statemanager.attach(1st) and statemanager.attach(2nd).
Then 2nd.setEnabled(false); To make sure it doesn’t run.
1st app will listen to key events and when “esc” it calls statemanager.getState(2nd).setEnabled(true) to activate it and 1st.setEnabled(false) to deactive while help menu is running.
Then 2nd app listens to “esc” and if hit. calls statemanager.getState(1st).setEnabled(true) and 2nd.setEnabled(false).
When that happens, 1st. as soon as onEnabled is called next “onAction” on “Esc”.
I only hit the escape once, not sure if that once is showing many hits in the keyevent system or not and it is just processing many of them.
This is possible if you aren’t checking the ActionListeners
isPressed boolean to determine if you want to run the code when the click is pressed down or when its released.
Also an easy way to check if its running more than once would be to put a System.out.println() with your code that runs when escape is pressed, and see if it your debug text gets printed out more than once per press.
Thanks. That helped, I figured it out.
Some of the baseAppState onAction was not checking “isPressed” so on pressing it did it and the second appState was checking on !isPressed.
So It was grabbing both, one for the pressed and the second was on not isPressed.
Changed it to both not isPressed and it stopped.