The first thing we will do is to construct a list that contains all currently active touch objects and their locations. It will use its own touch listener to stay up to date with any touch event that reaches the Runtime
target (so buttons and other objects that return true in their touch listeners will not affect it).
After you've copied your project directory from the version 0
contents, create a text file at the top level of your project called touches.lua
and open it.
This module will allow the user to iterate through all the currently active touches.
Since this file exists to track multiple simultaneous touches, it should guarantee that Corona will track these touches; by default, Corona operates in single-touch mode.
system.activate("multitouch")
We will need a table to track touches across individual event calls:
system.activate("multitouch")
local touchList = {}
We'll need to use a touch listener on the Runtime
target to follow individual touches:
local touchList = {} local function process( event ) end Runtime:addEventListener('touch', process)
When a touch begins, add its location info to the touches list:
local function process( event )
if event.phase == 'began' then
touchList[event.id] = {x = event.x, y = event.y; xStart = event.xStart, yStart = event.yStart}
end
end
When a touch moves around the screen, we make sure that it's one we're tracking (in case something else intercepted the beginning of a touch), and that we update the correct info:
touchList[event.id] = {x = event.x, y = event.y; xStart = event.xStart, yStart = event.yStart} elseif event.phase = 'moved' then local touch = touchList[event.id] if touch then end end
If we recognize the touch, we update it with the current location:
if touch then
touch.x, touch.y = event.x, event.y
end
Touch events that are not moved or begun are either ended or cancelled, either of which means that the touch is over:
touch.x, touch.y = event.x, event.y end else touchList[event.id] = nil end
This list allows us to check the status of all active touches whenever we need to, by centralizing responses to the touch events rather than trying to process each one as it comes in. This also means that we have access to each touch that hasn't received any events since the last cycle.
3.21.76.0