For a project of mine I needed some simple code to detect swipes. I’ve seen people ask how to do it before, so I figured I’d go ahead and share it here (along with a simple code in main showing how it’s used)
Basically it works as so: A swipe must be of a certain length and time and not distance too far vertically or horizontally depending on the direction of the swipe. You can call Swipes:getSwipe()
and it will return a vector based on how much you swiped (Ex a long swipe right would return vec2(3.2, 0) whereas a short swipe down would return vec2(0, -1.3)
Enough talking, here’s the code:
--# Main
-- Swiper
-- Use this function to perform your initial setup
function setup()
swiper = Swipes()
x = WIDTH / 2
y = HEIGHT / 2
end
-- This function gets called once every frame
function draw()
background(255, 255, 255, 255)
stroke(0, 155, 255, 255) strokeWidth(2) noFill()
local drawX, drawY = x % WIDTH, y % HEIGHT
ellipse(drawX, drawY, 100)
end
function touched(t)
-- Pass touch along to swipe handler
swiper:touched(t)
if t.state == ENDED then
-- Get the swipe and use it to move the ball
local s = swiper:getSwipe()
tween(math.abs(0.2 * s.x), _G, { x = x + s.x * 100 }, tween.easing.linear)
tween(math.abs(0.2 * s.y), _G, { y = y + s.y * 100 }, tween.easing.linear)
end
end
--# Swipes
Swipes = class()
function Swipes:init()
self.allowedMargin = WIDTH / 10
self.swipeLen = WIDTH / 21
self.allowedTime = 2
self.firstTouch = nil
self.lastTouch = nil
self.startTime = nil
self.endTime = nil
self.swipe = vec2(0, 0)
end
function Swipes:getSwipe()
local ret = self.swipe or vec2(0,0)
self.swipe = vec2(0,0)
return ret
end
function Swipes:findSwipes()
if self.startTime == nil or self.endTime == nil or self.endTime - self.startTime > self.allowedTime then return end
if self.firstTouch == nil or self.lastTouch == nil then self.firstTouch = nil self.lastTouch = nil return end
if self.lastTouch.x - self.firstTouch.x >= self.swipeLen and math.abs(self.lastTouch.y - self.firstTouch.y) <= self.allowedMargin then
-- Swiped right
local amp = (self.lastTouch.x - self.firstTouch.x) / self.swipeLen
self.swipe = vec2(amp, 0)
elseif self.firstTouch.x - self.lastTouch.x >= self.swipeLen and math.abs(self.lastTouch.y - self.firstTouch.y) <= self.allowedMargin then
-- Swiped left
local amp = (self.lastTouch.x - self.firstTouch.x) / self.swipeLen
self.swipe = vec2(amp, 0)
elseif self.lastTouch.y - self.firstTouch.y >= self.swipeLen and math.abs(self.lastTouch.x - self.firstTouch.x) <= self.allowedMargin then
-- Swiped up
local amp = (self.lastTouch.y - self.firstTouch.y) / self.swipeLen
self.swipe = vec2(0, amp)
elseif self.firstTouch.y - self.lastTouch.y >= self.swipeLen and math.abs(self.lastTouch.x - self.firstTouch.x) <= self.allowedMargin then
-- Swiped down
local amp = (self.lastTouch.y - self.firstTouch.y) / self.swipeLen
self.swipe = vec2(0, amp)
end
self.firstTouch = nil
self.lastTouch = nil
end
function Swipes:touched(t)
if t.state == BEGAN and self.firstTouch == nil then
self.firstTouch = t
self.startTime = ElapsedTime
end
if t.state == ENDED and self.lastTouch == nil then
self.lastTouch = t
self.endTime = ElapsedTime
self:findSwipes()
end
end