Hi all,
I got something working the way I need it, based purely on speed of swiping! Feel free to try it out - code below, no assets needed!
The code below is designed for an iPad Air. It will work on smaller devices as well, but maybe the specific cut-off speeds need to be tweaked based on screen size.
FYI: to test, swipe in short, fast strokes, up / down / left / right, and pause in between without lifting your finger off the screen.
main.lua:
-- main.lua ----------- display.setStatusBar( display.HiddenStatusBar ) -- hide the status bar local debugText = display.newText("Text", 768,960, native.systemFont, 200) debugText.alpha = 0 local fadeText = function(string) debugText.text = string debugText.alpha = 1 transition.to(debugText, {time = 250, alpha = 0}) end -- fadeText local lastX local lastY local lastTime local swipeDirection = false local lastDeltaX = 0 local lastDeltaY = 0 local screenTouched = function(event) if event.phase == "began" then lastX = event.x lastY = event.y lastTime = event.time elseif event.phase == "moved" then deltaX = event.x - lastX deltaY = event.y - lastY deltaTime = event.time - lastTime local xSpeed = deltaX / deltaTime local ySpeed = deltaY / deltaTime --print(ySpeed) if swipeDirection == false then local xSpeedAbs = math.abs(xSpeed) local ySpeedAbs = math.abs(ySpeed) if xSpeedAbs \> ySpeedAbs then if xSpeed \> 0.8 then swipeDirection = "right" fadeText("right") elseif xSpeed \< -0.8 then swipeDirection = "left" fadeText("left") end else -- ySpeedAbs =\> xSpeedAbs if ySpeed \> 0.8 then swipeDirection = "down" fadeText("down") elseif ySpeed \< -0.8 then swipeDirection = "up" fadeText("up") end end else -- swipeDirection is set in some direction if (math.abs(xSpeed) \< 0.06 and math.abs(ySpeed) \< 0.06) or lastDeltaX\*deltaX \<0 or lastDeltaY\*deltaY \<0 then swipeDirection = false lastDeltaX = 0 lastDeltaY = 0 end end lastX = event.x lastY = event.y lastTime = event.time lastDeltaX = deltaX lastDeltaY = deltaY elseif event.phase == "ended" or event.phase == "cancelled" then swipeDirection = false lastDeltaX = 0 lastDeltaY = 0 end end -- screenTouched Runtime:addEventListener("touch", screenTouched)
config.lua:
application = { launchPad = true, content = { fps = 60, audioPlayFrequency = 44100, width = 1536, height = 2048, scale = "letterBox", antialias = true, }, }
build settings:
settings = { orientation = { default = "portrait", supported = { "portrait", "landscapeLeft", "landscapeRight" }, }, iphone = { plist = { UIAppFonts = { "Montserrat-Bold.otf", "carbon bl.ttf" }, UIPrerenderedIcon = false, UIStatusBarHidden = true, CFBundleIconFile = "Icon.png", CFBundleIconFiles = { "Icon.png", --57x57 (iPhone and iPod touch application icon) "Icon@2x.png", --114x114 (RETINA iPhone and iPod touch application icon) "Icon-iPad.png", --72x72 (iPad 1 application icon) "Icon-72.png", --72X72 (RETINA iPad 2 OLD name) "Icon-iPad@2x.png", --144x144 (RETINA iPad 3) "Icon-Small.png", --29x29 (iPhone and iPod touch Spotlight search results and Settings icon) "Icon-Small@2x.png", --58x58 (RETINA iPhone and iPod touch Spotlight search results and Settings icon) "Icon-Small-50.png", --50x50 (iPad 1 Spotlight search results and Settings icon) "Icon-Small-50@2x.png", --100x100 (RETINA iPads Spotlight search results and Settings icon) }, }, }, }