local arr = {} 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") transition.to(arr[id][id2],{time= 5000 , x = 0}) end else totalSwipeDistanceRight = endX - beginX if totalSwipeDistanceRight \> minSwipeDistance then print("Swiped Right") transition.to(arr[id][id2],{time= 5000 , x = 300}) end end else if beginY \> endY then totalSwipeDistanceUp = beginY - endY if totalSwipeDistanceUp \> minSwipeDistance then print("Swiped Up") transition.to(arr[id][id2],{time= 5000 , y = 0}) end else totalSwipeDistanceDown = endY - beginY if totalSwipeDistanceDown \> minSwipeDistance then print("Swiped Down") transition.to(arr[id][id2],{time= 5000 , y = 500}) end end end end end function swipe(event) if event.phase == "began" then id = event.target.id id2 = event.target.id2 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 local arr2 = {{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,1,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0},} for i = 1 , 4 do arr[i] = {} for j = 1 , 7 do if arr2[j][i] == 0 then else arr[i][j] = display.newRect(0,0,75,50) arr[i][j].x = i\*76 - 30 arr[i][j].y = j \*51 +70 arr[i][j].id = i arr[i][j].id2 = j arr[i][j]:addEventListener("touch", swipe) end end end
How to move object using swipe ?