Skip to content

Instantly share code, notes, and snippets.

@robmiracle
Created August 6, 2015 02:20
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/3623601816a1a31436d1 to your computer and use it in GitHub Desktop.
Save robmiracle/3623601816a1a31436d1 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)
-- Since controllers don't generate constant values, but simply events when
-- the values change, we need to set a movement amount when the event happens,
-- and also have the game loop continuously apply it
-- We can also calculate our rotation angle here
local function moveRedPlayer()
-- Set the .isMovingX and .isMovingY values in our event handler
-- If this number isn't 0 (stopped moving), move the player
if ( redPlayer.isMovingX ~= 0 ) then
redPlayer.x = redPlayer.x + redPlayer.isMovingX
end
if ( redPlayer.isMovingY ~= 0 ) then
redPlayer.y = redPlayer.y + redPlayer.isMovingY
end
-- Rotation code
if ( redPlayer.rotationDistance > 0.1 ) then
if ( redPlayer.thisAngle > redPlayer.lastAngle ) then
redPlayer.rotation = redPlayer.rotation + redPlayer.rotationDistance
else
redPlayer.rotation = redPlayer.rotation - redPlayer.rotationDistance
end
end
end
Runtime:addEventListener( "enterFrame", moveRedPlayer )
local function moveGreenPlayer()
if ( greenPlayer.isMovingX ~= 0 ) then
greenPlayer.x = greenPlayer.x + greenPlayer.isMovingX
end
if ( greenPlayer.isMovingY ~= 0 ) then
greenPlayer.y = greenPlayer.y + greenPlayer.isMovingY
end
if ( greenPlayer.rotationDistance > 0.1 ) then
if ( greenPlayer.thisAngle > greenPlayer.lastAngle ) then
greenPlayer.rotation = greenPlayer.rotation + greenPlayer.rotationDistance
else
greenPlayer.rotation = greenPlayer.rotation - greenPlayer.rotationDistance
end
end
end
Runtime:addEventListener( "enterFrame", moveGreenPlayer )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment