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

Author Topic: About moving a sprite with a gamepad  (Read 3836 times)

0 Members and 1 Guest are viewing this topic.

fabvman

  • Newbie
  • *
  • Posts: 11
    • View Profile
About moving a sprite with a gamepad
« on: October 21, 2010, 04:31:32 pm »
Hi everyone. I have a little problem. I was reading the tutorials and I want to move a sprite on the screen using the gamepad, but I don't understand how to use joystick input (buffered or unbuffered), I mean, how to take and do something with I trigger some event. Can you give me some orientation about using class input and event?. Thanks!

ekun

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Vagante
    • Email
About moving a sprite with a gamepad
« Reply #1 on: October 29, 2010, 08:36:53 pm »
Hi fabvman.

Gamepad input is handled in the exact same way keyboard input is.

Code: [Select]

const sf::Input& Input = App.GetInput();

if(Input.IsJoystickButtonDown(0, 1))
{
   //do something
}
if(Input.GetJoystickAxis(0, sf::Joy::AxisX < -0.5))
{
   //move my guy left
}


All you have to keep in mind is the first argument for isJoyStickButtonDown() and GetJoyStickAxis() is the controller number. The second argument is which button/axis you are trying to test.
@ekunenuke

fabvman

  • Newbie
  • *
  • Posts: 11
    • View Profile
About moving a sprite with a gamepad
« Reply #2 on: October 29, 2010, 11:16:37 pm »
Thanks Epaik, that helps me a lot. But I don't understand why you put < -0.5 as a second parameter of getjoyaxis()

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
About moving a sprite with a gamepad
« Reply #3 on: October 30, 2010, 12:11:49 am »
He just forgot a closing parenthesis after "sf::Joy::AxisX".
Laurent Gomila - SFML developer

ekun

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Vagante
    • Email
About moving a sprite with a gamepad
« Reply #4 on: October 30, 2010, 04:21:36 am »
Quote from: "Laurent"
He just forgot a closing parenthesis after "sf::Joy::AxisX".


Good catch.

Quote from: "fabvman"
Thanks Epaik, that helps me a lot. But I don't understand why you put < -0.5 as a second parameter of getjoyaxis()


The reason I did -0.5 is the getjoyaxis function returns an approximate float value for each axis. For example, for a gamepad the x-axis might be ~-1.0 for left, ~0 for nothing pressed, ~1.0 for right.
@ekunenuke