Skip to content

Instantly share code, notes, and snippets.

@robmiracle
Created August 6, 2015 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robmiracle/409c3c16bcc65767e30a to your computer and use it in GitHub Desktop.
Save robmiracle/409c3c16bcc65767e30a 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)
local function onAxisEvent( event )
-- Display some info on the screen about this axis event
local message = "Axis '" .. event.axis.descriptor .. "' was moved " .. tostring( event.normalizedValue )
myAxisDisplayText.text = message
-- Map event data to simple variables
local abs = math.abs
local controller = event.device.descriptor
local thisAxis = event.axis.number
local thisPlayer
-- Check which controller this is coming from; you can trust the names
-- "Joystick 1" and "Joystick 2" to represent player 1, player 2, etc.
-- Based on the controller for this event, pick the object to manipulate
if ( controller == "Joystick 1" ) then
thisPlayer = redPlayer
elseif ( controller == "Joystick 2" ) then
thisPlayer = greenPlayer
end
-- Now that we know which controller it is, determine which axis to measure
-- Because the "right trigger" might be 6 on one brand of controller
-- but 14 on another, we use the mapping system described above
if ( axis[controller]["left_x"] and axis[controller]["left_x"] == thisAxis ) then
-- This helps handle noisy sticks and sticks that don't settle back to 0 exactly
-- You can adjust the value based on the sensitivity of the stick
-- If the stick is moved far enough, then move the player, else force it to
-- settle back to a zero value
-- Set the X distance in the player object so the enterFrame function can move it
if ( abs(event.normalizedValue) > 0.15 ) then
thisPlayer.isMovingX = event.normalizedValue
else
thisPlayer.isMovingX = 0
end
-- Draw the blue dot around the center to show how far you actually moved the stick
blueDot.x = display.contentCenterX + event.normalizedValue * 10
elseif ( axis[controller]["left_y"] and axis[controller]["left_y"] == thisAxis ) then
-- Just like X, now handle the Y axis
if ( abs(event.normalizedValue) > 0.15 ) then
thisPlayer.isMovingY = event.normalizedValue
else
thisPlayer.isMovingY = 0
end
-- Move the blue dot
blueDot.y = display.contentCenterY + event.normalizedValue * 10
elseif ( axis[controller]["right_x"] and axis[controller]["right_x"] == thisAxis ) then
-- We will use the right stick to rotate our player
thisPlayer.isRotatingX = event.normalizedValue
-- Use Pythagoras' Theorem to compute the distance the stick is moved from center
local a = math.abs( thisPlayer.isRotatingX * thisPlayer.isRotatingX )
local b = math.abs( thisPlayer.isRotatingY * thisPlayer.isRotatingY )
local d = math.sqrt( a + b )
-- If the distance isn't very far, set it to zero to account for
-- stick "slop" and not settling back to perfect center
if ( d < 0.15 ) then
thisPlayer.rotationDistance = 0
else
-- In the Runtime enterFrame listener we look at the current angle and the
-- last angle to determine which direction we need to rotate
thisPlayer.rotationDistance = d * 3
thisPlayer.lastAngle = thisPlayer.thisAngle
thisPlayer.thisAngle = math.floor( calculateAngle(thisPlayer.isRotatingX, thisPlayer.isRotatingY) )
end
elseif ( axis[controller]["right_y"] and axis[controller]["right_y"] == thisAxis ) then
-- Repeat for the Y axis on the right stick
thisPlayer.isRotatingY = event.normalizedValue
local a = math.abs( thisPlayer.isRotatingX * thisPlayer.isRotatingX )
local b = math.abs( thisPlayer.isRotatingY * thisPlayer.isRotatingY )
local d = math.sqrt( a + b )
if ( d < 0.15 ) then
thisPlayer.rotationDistance = 0
else
thisPlayer.rotationDistance = d * 3
thisPlayer.lastAngle = thisPlayer.thisAngle
thisPlayer.thisAngle = math.floor( calculateAngle(thisPlayer.isRotatingX, thisPlayer.isRotatingY) )
end
elseif ( axis[controller]["left_trigger"] or axis[controller]["right_trigger"] == thisAxis ) then
-- Use the analog triggers to gradually change the color of the player
-- No trigger pressure will be full brightness
-- The more you squeeze the trigger, the darker the square gets
local color = 1 * (1 - event.normalizedValue)
if ( color < 0.125 ) then
color = 0.125
elseif ( color >= 1 ) then
color = 1
end
if ( thisPlayer.color == "red" ) then
thisPlayer:setFillColor( color, 0, 0 )
else
thisPlayer:setFillColor( 0, color, 0 )
end
end
return true
end
Runtime:addEventListener( "axis", onAxisEvent )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment