SFML community forums

Help => Window => Topic started by: fabvman on October 21, 2010, 04:31:32 pm

Title: About moving a sprite with a gamepad
Post by: fabvman 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!
Title: About moving a sprite with a gamepad
Post by: ekun 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.
Title: About moving a sprite with a gamepad
Post by: fabvman 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()
Title: About moving a sprite with a gamepad
Post by: Laurent on October 30, 2010, 12:11:49 am
He just forgot a closing parenthesis after "sf::Joy::AxisX".
Title: About moving a sprite with a gamepad
Post by: ekun 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.