Multiple states with same listener map probelm

I am encountering a problem with states and listeners.

I have two states which are mutually exclusive. Pressing the ‘esc’ key attaches one state, and detaches the other. My problem is that when I press ‘esc’ State A get’s detached and state B gets attached and registers it’s ‘esc’ listener. State B’s escape listener then fires off of the initial press which attached it.

Does that make sense? Is there an easy way to prevent this?

It sounds like one is getting the down and the other is getting the up. You should respond to only up probably.

If not, then try marking the event as consumed.

It’s not the up/down state, because I have a wrapper in both the action to only continue for the down state.

I did not know event’s could be marked as consumed, so that is something maybe I can try later today. I am still not certain it will work. The esc key is mapped to two different events “Resume_Game” and “Pause_Game”. State A is listening for “Resume_Game”, state B is listening for “Pause_Game”.

Thanks for the quick response pspeed!

Wait… when are you adding the listener?

Note: if you ever override stateAttached and stateDetached then you are doing it wrong. There is almost never a good reason to implement these methods.

And if you are registering the listener in initialize() then it’s not going to happen until the next update() loop… so I don’t know how you could be receiving an event unless it’s the key up. The key down should already be long gone by then.

I am using stateAttached and stateDetached functions! It seemed like a clean way to organize listeners by state.

Should my onAction function be checking for the presence of a state instead?

@beave2111 said: I am using stateAttached and stateDetached functions! It seemed like a clean way to organize listeners by state.

Should my onAction function be checking for the presence of a state instead?

Do not use stateAttached() and stateDetached(). These are mostly only useful for debugging and exactly parallel initialize() and cleanup()… except that stateAttached() is called before initialize() and on any random thread that attach was called on and stateDetached() is called before cleanup() and on any random thread detach was called on.

initialize() and cleanup() are the proper methods.

1 Like

This sounds promising. I will try tonight. Thank you.

Pspeed, your advice made all my dreams come true. Thanks again.

1 Like