Tracking multiple touches

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).

Getting ready

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.

Getting on with it

This module will allow the user to iterate through all the currently active touches.

Enabling multiple 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")

Note

Testing multi-touch events requires building for device; since the simulator emulates "touch" input based on the position of the mouse, it can't generate multiple touch points at once.

Creating a list

We will need a table to track touches across individual event calls:

system.activate("multitouch")

local touchList = {}

Adding new entries to the list

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

Note

Touch events contain an ID field, which contains a value that identifies the touch. This value will be the same for all touch events that are part of the same gesture or finger contact.

Updating entries in the list

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

Clearing entries from the list

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

Granting access to the list

We won't grant direct access to the list to avoid making it easy for coders relying on the module to damage it, but instead we will pass a function that iterates over the list's contents:

Runtime:addEventListener('touch', process)

return function()
  return pairs(touchList)
end

What did we do?

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.76.0