Part of the problem is that you aren’t programmatically determining which you want to do. If you touch a place on the screen with no other input, the app has no way to decide on it’s own which to run. If you assigned the touch events to different objects you could intercept the touch in one by returning true- but again, without any way to tell what the user is trying to do, all you’ll end up doing is forever disabling one of the two actions.
In this situation the touchScroll routine is always going to run, but you can use timers to decide whether the user is moving the character or moving the camera. A tap is just a really short touch.
local timer local tap = function(event) if event.phase == "began" then timer = system.getTimer() elseif event.phase == "ended" then local time = system.getTimer() - timer if time \< 100 then print("tap") --move the player else print("touch") --do nothing end end end mapObj:addEventListener("touch", tap)
You may have to fiddle with the time threshold a little. I chose 100 ms because it was a nice round, short number.
That just leaves the touchScroll code to take care of. The same basic principle applies; we want the touchScroll routine to ignore a short touch and obey a long touch. You’ll have to make a small modification to mte.lua:
Go to line 190, just above “local touchScrollPinchZoom = function(event)” and add:
local timer
Go to line 204 or so. It should read “if “began” == phase then”
Beneath it, add:
timer = system.getTimer()
Go to line 8975, the update2 function. You’ll see this code block:
if touchScroll[1] and touchScroll[6] then local velX = (touchScroll[2] - touchScroll[4]) / masterGroup.xScale local velY = (touchScroll[3] - touchScroll[5]) / masterGroup.yScale --print(velX, velY) M.moveCamera(velX, velY) touchScroll[2] = touchScroll[4] touchScroll[3] = touchScroll[5] end
Replace it with this:
if touchScroll[1] and touchScroll[6] then local velX = (touchScroll[2] - touchScroll[4]) / masterGroup.xScale local velY = (touchScroll[3] - touchScroll[5]) / masterGroup.yScale local time = system.getTimer() - timer if time \>= 100 then --print(velX, velY) M.moveCamera(velX, velY) end touchScroll[2] = touchScroll[4] touchScroll[3] = touchScroll[5] end
Now the camera won’t move unless the touch lasts longer than 100 ms. The touchScroll event will still fire, it just won’t do anything. You may have to modify the code here and there to your need, by changing the threshold time for example, or making it so that a second touch on the screen doesn’t reset the timer, but this is a good starting point.