Here is a way to detect swipe direction:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | local beginX local beginY local endX local endY local xDistance local yDistance function checkSwipeDirection() 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 print("swipe left") else print("swipe right") end else if beginY > endY then print("swipe up") else print("swipe down") end end end function swipe(event) if event.phase == "began" then beginX = event.x beginY = event.y end if event.phase == "ended" then endX = event.x endY = event.y checkSwipeDirection(); end end Runtime:addEventListener("touch", swipe) |
Thanks, I added a bit to stop swipe trigger/error onload and made a minimum swipe distance check..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | local beginX local beginY local endX local endY local xDistance local yDistance local bDoingTouch local minSwipeDistance = 50 local totalSwipeDistanceLeft local totalSwipeDistanceRight local totalSwipeDistanceUp local totalSwipeDistanceDown function checkSwipeDirection() 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 > minSwipeDistance then print("Swiped Left") end else totalSwipeDistanceRight = endX - beginX if totalSwipeDistanceRight > minSwipeDistance then print("Swiped Right") end end else if beginY > endY then totalSwipeDistanceUp = beginY - endY if totalSwipeDistanceUp > minSwipeDistance then print("Swiped Up") end else totalSwipeDistanceDown = endY - beginY if totalSwipeDistanceDown > minSwipeDistance then print("Swiped Down") end end end end end function swipe(event) if event.phase == "began" then bDoingTouch = true beginX = event.x beginY = event.y end if event.phase == "ended" then endX = event.x endY = event.y checkSwipeDirection(); bDoingTouch = false end end Runtime:addEventListener("touch", swipe) |
thanks too!
Hi NEITAFE,
Thanks for those additions - great ideas !! Nice work.
Mark
This looks really useful. Thanks!
thankS