Thursday, June 11, 2015

c++ - Action button: only true once per press


I'm using SFML2.0 and am trying to make a wrapper class for my controller/joystick. I read all the input data from my controller and send it off to my controllable object.


I want to have two types of buttons per button press, one that is continues(true false state ) and one that is an action and is set to false after the next frame update.


Here is an example of how I set my button A to true or false with the SFML api. Whereas data is my struct of buttons, and A holds my true/false state every update.


data.A = sf::Joystick::isButtonPressed(i,st::input::A);

But I've also added "data.actionA" which represents the one time action state.



Basically what I want is for actionA to be set false after the update its been set to true. I'm trying to keep track of the previous state. But I seem to fall into this loop where it toggles between true and false every update.


Anyone an idea?



Answer



Here's some pseudo code for you to try


// This here is a global variable.
...
bool wasButtonPressed = false;
...

// This here is your function.

...
bool isButtonPressed = sf::Joystick::isButtonPressed(i,st::input::A);

// Check if the key was pressed since last frame, aka KeyPressed.
if(isButtonPressed && !wasButtonPressed)
someFunction();

// Check if the key was held since last frame, aka KeyHeld.
if(isButtonPressed && wasButtonPressed)
someFunction();


// Check if the key was released since last frame, aka KeyReleased.
if(!isButtonPressed && wasButtonPressed)
someFunction();

wasButtonPressed = isButtonPressed;
...

We store the previous state, so we know how the key has changed since the last frame.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...