Difference between revisions of "Joystick"
m |
m |
||
(8 intermediate revisions by 3 users not shown) | |||
Line 2: | Line 2: | ||
Represents a physical joystick. | Represents a physical joystick. | ||
== Constructors == | == Constructors == | ||
− | {{#ask: [[Category:Functions]] [[Constructs::Joystick]] | + | {{#ask: [[Category:Functions]] [[Constructs::Joystick]] [[Concept:Current]] |
| headers=hide | | headers=hide | ||
| default=None. | | default=None. | ||
Line 14: | Line 14: | ||
}} | }} | ||
== Functions == | == Functions == | ||
− | {{#ask: [[Category:Functions]] [[parent::Joystick||Object]] | + | {{#ask: [[Category:Functions]] [[parent::Joystick||Object]] [[Concept:Current]] |
| headers=hide | | headers=hide | ||
| format=template | | format=template | ||
Line 25: | Line 25: | ||
}} | }} | ||
== Enums == | == Enums == | ||
− | {{#ask: [[Category:Enums]] [[parent::Joystick]] | + | {{#ask: [[Category:Enums]] [[parent::Joystick]] [[Concept:Current]] |
| headers=hide | | headers=hide | ||
| format=template | | format=template | ||
Line 37: | Line 37: | ||
== Supertypes == | == Supertypes == | ||
* [[parent::Object]] | * [[parent::Object]] | ||
+ | == Examples == | ||
+ | === Display the last button pressed of a controller on-screen === | ||
+ | <source lang="Lua"> | ||
+ | local lastbutton = "none" | ||
+ | |||
+ | function love.gamepadpressed(joystick, button) | ||
+ | lastbutton = button | ||
+ | end | ||
+ | |||
+ | function love.draw() | ||
+ | love.graphics.print("Last gamepad button pressed: "..lastbutton, 10, 10) | ||
+ | end | ||
+ | </source> | ||
+ | === Move a circle with the d-pad of a controller === | ||
+ | <source lang="Lua"> | ||
+ | function love.load() | ||
+ | local joysticks = love.joystick.getJoysticks() | ||
+ | joystick = joysticks[1] | ||
+ | |||
+ | position = {x = 400, y = 300} | ||
+ | speed = 300 | ||
+ | end | ||
+ | |||
+ | function love.update(dt) | ||
+ | if not joystick then return end | ||
+ | |||
+ | if joystick:isGamepadDown("dpleft") then | ||
+ | position.x = position.x - speed * dt | ||
+ | elseif joystick:isGamepadDown("dpright") then | ||
+ | position.x = position.x + speed * dt | ||
+ | end | ||
+ | |||
+ | if joystick:isGamepadDown("dpup") then | ||
+ | position.y = position.y - speed * dt | ||
+ | elseif joystick:isGamepadDown("dpdown") then | ||
+ | position.y = position.y + speed * dt | ||
+ | end | ||
+ | end | ||
+ | |||
+ | function love.draw() | ||
+ | love.graphics.circle("fill", position.x, position.y, 50) | ||
+ | end | ||
+ | </source> | ||
+ | |||
+ | === Move circles and change sizes with sticks of gamepad === | ||
+ | <source lang="Lua"> | ||
+ | function love.load() | ||
+ | local joysticks = love.joystick.getJoysticks() | ||
+ | joystick = joysticks[1] | ||
+ | |||
+ | leftCircle = {x = 200, y = 280, size = 50} | ||
+ | rightCircle = {x = 600, y = 280, size = 50} | ||
+ | speed = 300 | ||
+ | end | ||
+ | |||
+ | function love.update(dt) | ||
+ | if not joystick then return end | ||
+ | |||
+ | leftCircle.x = leftCircle.x + dt*speed*joystick:getGamepadAxis("leftx") | ||
+ | leftCircle.y = leftCircle.y + dt*speed*joystick:getGamepadAxis("lefty") | ||
+ | |||
+ | rightCircle.x = rightCircle.x + dt*speed*joystick:getGamepadAxis("rightx") | ||
+ | rightCircle.y = rightCircle.y + dt*speed*joystick:getGamepadAxis("righty") | ||
+ | end | ||
+ | |||
+ | function love.draw() | ||
+ | local leftSize = (1-joystick:getGamepadAxis("triggerleft"))*leftCircle.size | ||
+ | local rightSize = (1-joystick:getGamepadAxis("triggerright"))*rightCircle.size | ||
+ | love.graphics.circle("fill", leftCircle.x, leftCircle.y, leftSize) | ||
+ | love.graphics.circle("fill", rightCircle.x, rightCircle.y, rightSize) | ||
+ | end | ||
+ | </source> | ||
+ | |||
== See Also == | == See Also == | ||
* [[parent::love.joystick]] | * [[parent::love.joystick]] | ||
* [[love.joystick.getJoystickCount]] | * [[love.joystick.getJoystickCount]] | ||
− | |||
* [[love.joystickadded]] | * [[love.joystickadded]] | ||
* [[love.joystickremoved]] | * [[love.joystickremoved]] | ||
Line 47: | Line 119: | ||
* [[love.joystickaxis]] | * [[love.joystickaxis]] | ||
* [[love.joystickhat]] | * [[love.joystickhat]] | ||
− | |||
* [[love.gamepadpressed]] | * [[love.gamepadpressed]] | ||
* [[love.gamepadreleased]] | * [[love.gamepadreleased]] | ||
* [[love.gamepadaxis]] | * [[love.gamepadaxis]] | ||
+ | |||
[[Category:Types]] | [[Category:Types]] | ||
{{#set:Description=Represents a physical joystick.}} | {{#set:Description=Represents a physical joystick.}} | ||
+ | |||
== Other Languages == | == Other Languages == | ||
{{i18n|Joystick}} | {{i18n|Joystick}} |
Latest revision as of 01:49, 13 November 2023
Available since LÖVE 0.9.0 |
This type is not supported in earlier versions. |
Represents a physical joystick.
Contents
Constructors
love.joystick.getJoysticks | Gets a list of connected Joysticks. | 0.9.0 |
Functions
Joystick:getAxes | Gets the direction of each axis. | 0.9.0 | |
Joystick:getAxis | Gets the direction of an axis. | 0.9.0 | |
Joystick:getAxisCount | Gets the number of axes on the joystick. | 0.9.0 | |
Joystick:getButtonCount | Gets the number of buttons on the joystick. | 0.9.0 | |
Joystick:getDeviceInfo | Gets the OS-independent device info of the joystick. | 11.3 | |
Joystick:getGUID | Gets a stable GUID unique to the type of the physical joystick. | 0.9.0 | |
Joystick:getGamepadAxis | Gets the direction of a virtual gamepad axis. | 0.9.0 | |
Joystick:getGamepadMapping | Gets the button, axis or hat that a virtual gamepad input is bound to. | 0.9.0 | |
Joystick:getGamepadMappingString | Gets the full gamepad mapping string of this Joystick, or nil if it's not recognized as a gamepad. | 11.3 | |
Joystick:getHat | Gets the direction of a hat. | 0.9.0 | |
Joystick:getHatCount | Gets the number of hats on the joystick. | 0.9.0 | |
Joystick:getID | Gets the joystick's unique identifier. | 0.9.0 | |
Joystick:getName | Gets the name of the joystick. | 0.9.0 | |
Joystick:getVibration | Gets the current vibration motor strengths on a Joystick with rumble support. | 0.9.0 | |
Joystick:isConnected | Gets whether the Joystick is connected. | 0.9.0 | |
Joystick:isDown | Checks if a button on the Joystick is pressed. | 0.9.0 | |
Joystick:isGamepad | Gets whether the Joystick is recognized as a gamepad. | 0.9.0 | |
Joystick:isGamepadDown | Checks if a virtual gamepad button on the Joystick is pressed. | 0.9.0 | |
Joystick:isVibrationSupported | Gets whether the Joystick supports vibration. | 0.9.0 | |
Joystick:setVibration | Sets the vibration motor speeds on a Joystick with rumble support. | 0.9.0 | |
Object:release | Immediately destroys the object's Lua reference. | 11.0 | |
Object:type | Gets the type of the object as a string. | ||
Object:typeOf | Checks whether an object is of a certain type. |
Enums
GamepadAxis | Virtual gamepad axes. | 0.9.0 | |
GamepadButton | Virtual gamepad buttons. | 0.9.0 | |
JoystickHat | Joystick hat positions. | 0.5.0 | |
JoystickInputType | Types of Joystick inputs. | 0.9.0 |
Supertypes
Examples
Display the last button pressed of a controller on-screen
local lastbutton = "none"
function love.gamepadpressed(joystick, button)
lastbutton = button
end
function love.draw()
love.graphics.print("Last gamepad button pressed: "..lastbutton, 10, 10)
end
Move a circle with the d-pad of a controller
function love.load()
local joysticks = love.joystick.getJoysticks()
joystick = joysticks[1]
position = {x = 400, y = 300}
speed = 300
end
function love.update(dt)
if not joystick then return end
if joystick:isGamepadDown("dpleft") then
position.x = position.x - speed * dt
elseif joystick:isGamepadDown("dpright") then
position.x = position.x + speed * dt
end
if joystick:isGamepadDown("dpup") then
position.y = position.y - speed * dt
elseif joystick:isGamepadDown("dpdown") then
position.y = position.y + speed * dt
end
end
function love.draw()
love.graphics.circle("fill", position.x, position.y, 50)
end
Move circles and change sizes with sticks of gamepad
function love.load()
local joysticks = love.joystick.getJoysticks()
joystick = joysticks[1]
leftCircle = {x = 200, y = 280, size = 50}
rightCircle = {x = 600, y = 280, size = 50}
speed = 300
end
function love.update(dt)
if not joystick then return end
leftCircle.x = leftCircle.x + dt*speed*joystick:getGamepadAxis("leftx")
leftCircle.y = leftCircle.y + dt*speed*joystick:getGamepadAxis("lefty")
rightCircle.x = rightCircle.x + dt*speed*joystick:getGamepadAxis("rightx")
rightCircle.y = rightCircle.y + dt*speed*joystick:getGamepadAxis("righty")
end
function love.draw()
local leftSize = (1-joystick:getGamepadAxis("triggerleft"))*leftCircle.size
local rightSize = (1-joystick:getGamepadAxis("triggerright"))*rightCircle.size
love.graphics.circle("fill", leftCircle.x, leftCircle.y, leftSize)
love.graphics.circle("fill", rightCircle.x, rightCircle.y, rightSize)
end
See Also
- love.joystick
- love.joystick.getJoystickCount
- love.joystickadded
- love.joystickremoved
- love.joystickpressed
- love.joystickreleased
- love.joystickaxis
- love.joystickhat
- love.gamepadpressed
- love.gamepadreleased
- love.gamepadaxis
Other Languages
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info