1
\$\begingroup\$

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?

\$\endgroup\$
0

2 Answers 2

3
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Yes I already solved it myself ( see edit ). I was already keeping track of previous states. I was overthinking it. \$\endgroup\$
    – Sidar
    Commented Jun 20, 2012 at 16:08
0
\$\begingroup\$

My solution:

data.actionA = data.A = sf::Joystick::isButtonPressed(i,st::input::A);
if(prev.A) data.actionA = false;

First I always set the actionA to the value of the button state. Then I check if the previous state of A is true. If so we negate the value.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .