Difference between revisions of "Joystick:getGamepadAxis"

m
(Added example.)
Line 10: Line 10:
 
=== Returns ===
 
=== Returns ===
 
{{param|number|direction|Current value of the axis.}}
 
{{param|number|direction|Current value of the axis.}}
 +
== Examples ==
 +
=== Move x and y values based on a gamepad thumbstick ===
 +
<source lang="lua">
 +
function love.load()
 +
    x = 0
 +
    y = 0
 +
    p1joystick = nil
 +
end
 +
 +
function love.joystickadded(joystick)
 +
    p1joystick = joystick
 +
end
 +
 +
function love.update(dt)
 +
    -- Check if joystick connected
 +
    if p1joystick != nil then
 +
        -- getGamepadAxis returns a value between -1 and 1.
 +
        -- It returns 0 when it is at rest
 +
 +
        x = x + p1joystick:getGamepadAxis("leftx")
 +
        y = y + p1joystick:getGamepadAxis("lefty")
 +
    end
 +
end
 +
</source>
 
== See Also ==
 
== See Also ==
 
* [[parent::Joystick]]
 
* [[parent::Joystick]]

Revision as of 16:28, 18 November 2014

Available since LÖVE 0.9.0
This function is not supported in earlier versions.

Gets the direction of a virtual gamepad axis. If the Joystick isn't recognized as a gamepad or isn't connected, this function will always return 0.

Function

Synopsis

direction = Joystick:getGamepadAxis( axis )

Arguments

GamepadAxis axis
The virtual axis to be checked.

Returns

number direction
Current value of the axis.

Examples

Move x and y values based on a gamepad thumbstick

function love.load()
    x = 0
    y = 0
    p1joystick = nil
end

function love.joystickadded(joystick)
    p1joystick = joystick
end

function love.update(dt)
    -- Check if joystick connected
    if p1joystick != nil then
        -- getGamepadAxis returns a value between -1 and 1.
        -- It returns 0 when it is at rest

        x = x + p1joystick:getGamepadAxis("leftx")
        y = y + p1joystick:getGamepadAxis("lefty")
    end
end

See Also

Other Languages