Created
August 6, 2015 02:23
-
-
Save robmiracle/8f66d2f5887ff80bcd16 to your computer and use it in GitHub Desktop.
Code snippets for the Corona SDK Tutorial: Game controllers and axes (https://coronalabs.com/blog/2013/09/24/tutorial-controllers-and-axis)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Fetch all input devices currently connected to the system | |
local inputDevices = system.getInputDevices() | |
-- Traverse all input devices | |
for deviceIndex = 1, #inputDevices do | |
-- Fetch the input device's axes | |
print( deviceIndex, "andoridDeviceid", inputDevices[deviceIndex].androidDeviceId ) | |
print( deviceIndex, "canVibrate", inputDevices[deviceIndex].canVibrate ) | |
print( deviceIndex, "connectionState", inputDevices[deviceIndex].connectionState ) | |
print( deviceIndex, "descriptor", inputDevices[deviceIndex].descriptor ) | |
print( deviceIndex, "displayName", inputDevices[deviceIndex].displayName ) | |
print( deviceIndex, "isConnected", inputDevices[deviceIndex].isConnected ) | |
print( deviceIndex, "permenantid", tostring(inputDevices[deviceIndex].permanentId) ) | |
print( deviceIndex, "type", inputDevices[deviceIndex].type ) | |
-- OUYA may append the controller name to the end of the display name in a future update | |
-- Future-proof this by looking at the first few characters and, if necessary, parse it | |
local displayName = inputDevices[deviceIndex].displayName | |
if ( string.sub(displayName,1,20) == "OUYA Game Controller" then | |
displayName = string.sub( displayName,1,20 ) | |
end | |
local descriptor = inputDevices[deviceIndex].descriptor | |
local inputAxes = inputDevices[deviceIndex]:getAxes() | |
-- Only look for Joysticks at the moment and map the controllers | |
if ( inputDevices[deviceIndex].type == "joystick" ) then | |
print( "We have a joystick; let's find some analog inputs!" ) | |
if ( #inputAxes > 0 ) then | |
local controller = 0 | |
for axisIndex = 1, #inputAxes do | |
if ( axisMap[displayName] and axisMap[displayName][axisIndex] ) then | |
axis[descriptor][axisMap[displayName][axisIndex]] = axisIndex | |
print( "mapped axis[" .. axisMap[displayName][axisIndex] .. "] to ", axisIndex ) | |
end | |
end | |
else | |
-- Device does not have any axes! | |
print( inputDevices[deviceIndex].descriptor .. ": No axes found." ) | |
end | |
else | |
print( "Not a Joystick" ) | |
end | |
end | |
-- Keys were handled in a previous blog post, but let's handle them to | |
-- demonstrate how some axis values map to key events | |
local function onKeyEvent( event ) | |
local phase = event.phase | |
local keyName = event.keyName | |
print( event.phase, event.keyName ) | |
local message = "Key '" .. event.keyName .. "' was pressed " .. event.phase | |
myKeyDisplayText.text = message | |
return false | |
end | |
Runtime:addEventListener( "key", onKeyEvent ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment