Jump to content
  • Advertisement
Sign in to follow this  
howie_007

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

This topic is 2765 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!