SFML community forums

Help => Window => Topic started by: charisma_ts on November 17, 2017, 07:09:30 am

Title: Problem with Button pressed
Post by: charisma_ts on November 17, 2017, 07:09:30 am
Hi, I want to detect when the joystick button was pressed. But, I have a problem that once a button is pressed, the button was detected to be pressed always. I used only once in the if statement sf::Joystick::isButtonPressed() but the debug message says button was pressed all the time.
I attached my screenshot of the program. What could cause it? and how to solve this.
Title: Re: Problem with Button pressed
Post by: achpile on November 17, 2017, 08:27:30 am
Because button was pressed for multiple "ticks". If you wanna handle event - handle sf::Event. Or implement own class kinda

newState = sf::Joystick::isButtonPressed

if (oldState != newState && newState) isPressed = true;
else isPressed = false;

oldState = newState
 
Title: Re: Problem with Button pressed
Post by: charisma_ts on November 17, 2017, 10:35:52 am
Because button was pressed for multiple "ticks". If you wanna handle event - handle sf::Event. Or implement own class kinda

newState = sf::Joystick::isButtonPressed

if (oldState != newState && newState) isPressed = true;
else isPressed = false;

oldState = newState
 
what is the value for oldState initially?
Title: Re: Problem with Button pressed
Post by: achpile on November 17, 2017, 10:47:21 am
obviously false
Title: Re: Problem with Button pressed
Post by: achpile on November 17, 2017, 10:47:49 am
or you can init it on constructor with current value of isButtonPressed
Title: Re: Problem with Button pressed
Post by: Laurent on November 17, 2017, 11:12:20 am
But using events is much better.
Title: Re: Problem with Button pressed
Post by: achpile on November 17, 2017, 11:19:29 am
But using events is much better.

Depending on situation :) I use keyboard and gamepad settings for same actions and it looks like this

bool ach::Control::check() {
        bool result = false;

        if (sf::Keyboard::isKeyPressed(keyCode)) {
                result = true;
        }

        if (!useAxis) {
                if (sf::Joystick::isButtonPressed(joyID, joyCode)) {
                        result = true;
                }
        } else {
                if ((movAxis ==  1.0f && sf::Joystick::getAxisPosition(joyID, joyAxis) >  GAMEPAD_SENSETIVITY) ||
                    (movAxis == -1.0f && sf::Joystick::getAxisPosition(joyID, joyAxis) < -GAMEPAD_SENSETIVITY)) {
                        result = true;
                }
        }


        return result;
}
 

void ach::ControlKey::update() {
        if (!ctrl) return;

        bool newState = ctrl->check();

        if ( newState && newState != state) pressed = true;
        else                                pressed = false;

        if (!newState && newState != state) released = true;
        else                                released = false;

        state = newState;
}
 
Title: Re: Problem with Button pressed
Post by: Laurent on November 17, 2017, 12:02:42 pm
Quote
Depending on situation
Yes, but since the OP really seems to understand very little about what he's doing, I'd avoid suggesting "complicated" custom solutions and focus on the obvious one first.

Quote
I use keyboard and gamepad settings for same actions and it looks like this
I have the feeling that this code would really look better with events.
You could also look at higher-level solutions such as Thor.Actions (http://www.bromeon.ch/libraries/thor/tutorials/v2.0/actions.html).
Title: Re: Problem with Button pressed
Post by: achpile on November 17, 2017, 12:09:56 pm
Quote
Yes, but since the OP really seems to understand very little about what he's doing, I'd avoid suggesting "complicated" custom solutions and focus on the obvious one first.

Oh, yes, sure  :)

Quote
I have the feeling that this code would really look better with events.

That's why I love SFML. You can achieve result by different ways and everyone can choose which one fits better :)
Title: Re: Problem with Button pressed
Post by: charisma_ts on November 17, 2017, 01:17:56 pm
Cool thanks that worked for me.