Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem with Button pressed  (Read 4950 times)

0 Members and 2 Guests are viewing this topic.

charisma_ts

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Problem with Button pressed
« 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.

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: Problem with Button pressed
« Reply #1 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
 

charisma_ts

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Problem with Button pressed
« Reply #2 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?


achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: Problem with Button pressed
« Reply #4 on: November 17, 2017, 10:47:49 am »
or you can init it on constructor with current value of isButtonPressed

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with Button pressed
« Reply #5 on: November 17, 2017, 11:12:20 am »
But using events is much better.
Laurent Gomila - SFML developer

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: Problem with Button pressed
« Reply #6 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;
}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with Button pressed
« Reply #7 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.
Laurent Gomila - SFML developer

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: Problem with Button pressed
« Reply #8 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 :)

charisma_ts

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Problem with Button pressed
« Reply #9 on: November 17, 2017, 01:17:56 pm »
Cool thanks that worked for me.