Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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