Sign in to follow this  

What is a good way to handle continuous button feed back?

This topic is 2549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In a game loop, when the controller is polled, the same button press can be acted on many times in a row. What would be a good way to act on a button press and ignore the remaining ones until the player stops pressing the button?

Share this post


Link to post
Share on other sites
One way to do it is create an GameInput class that abstracts what the player presses on the controller into useful DataTypes.

You can have two data types - PrevGameState and CurGameState. In each game loop, set PrevGameState = CurGameState, then update the CurGameState.

You can have a class called Button that has a few properties (Pressed, NewPress), which are determined using some logic and CurGameState and PrevGameState.

Share this post


Link to post
Share on other sites
It sounds like the easiest way is to have an array of booleans which correspond to a key, and if it's pressed/held down the button is set to true. Once the button is released, the button is set to false.


boolean keys[256];

void update() {
KeyEvent key = input.pollKeyboard();
if ( key.isDown ) {
keys[key.code] = true; // the button has been pressed
}
else {
keys[key.code] = false; // the button has been released
}

}


If you want to get fancier than that, add a timestamp to each key press. Only allow the button to be "pressed" if a certain amount of time has elapsed.

Share this post


Link to post
Share on other sites

This topic is 2549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this