Skip to content

Instantly share code, notes, and snippets.

@AGulev
Created July 29, 2016 06:25
Show Gist options
  • Save AGulev/5716fba821bf24084e25ebdebf4b0d1c to your computer and use it in GitHub Desktop.
Save AGulev/5716fba821bf24084e25ebdebf4b0d1c to your computer and use it in GitHub Desktop.
adoptation of swipe-direction script by ScottPhillips https://github.com/ScottPhillips/swipe-direction/blob/master/swipe-direction.lua for Defold engine using mouse_trigger instead touch_trigger
local beginX
local beginY
local endX
local endY
local startTime = 0
local xDistance
local yDistance
go.property("minSwipeDistance", 40)
go.property("minSwipeTime", 0.6)
local bDoingTouch
local totalSwipeDistanceLeft
local totalSwipeDistanceRight
local totalSwipeDistanceUp
local totalSwipeDistanceDown
function checkSwipeDirection(self)
if bDoingTouch == true then
xDistance = math.abs(endX - beginX) -- math.abs will return the absolute, or non-negative value, of a given value.
yDistance = math.abs(endY - beginY)
if xDistance > yDistance then
if beginX > endX then
totalSwipeDistanceLeft = beginX - endX
if totalSwipeDistanceLeft > self.minSwipeDistance then
print("Swiped Left")
end
else
totalSwipeDistanceRight = endX - beginX
if totalSwipeDistanceRight > self.minSwipeDistance then
print("Swiped Right")
end
end
else
if beginY > endY then
totalSwipeDistanceUp = beginY - endY
if totalSwipeDistanceUp > self.minSwipeDistance then
print("Swiped Down")
end
else
totalSwipeDistanceDown = endY - beginY
if totalSwipeDistanceDown > self.minSwipeDistance then
print("Swiped Up")
end
end
end
end
end
function init(self)
msg.post(".", "acquire_input_focus")
end
function final(self)
msg.post(".", "release_input_focus")
end
function on_input(self, action_id, action)
if action_id == hash("click") then
if action.pressed then
bDoingTouch = true
beginX = action.x
beginY = action.y
startTime = os.clock()
elseif action.released then
endX = action.x
endY = action.y
if os.clock() - startTime < self.minSwipeTime then
checkSwipeDirection(self);
end
bDoingTouch = false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment