Wiki

Clone wiki

Core / MultiTouch

Back to Codea's Example Projects


ProjectMultiTouch-Small.png Multi Touch by Dylan Sale

Introduction


Multi Touch is one of the example projects supplied with Codea. It is a simple example of the use of the multitouch interface. The example tracks multiple touches and colours them based on their unique ID.

The project runs in display mode STANDARD (by default) and supports any orientation.

Tabs in the project


All of the code is contained in the Main tab.

Main tab


The Main tab implements three callback functions (which are explained further later in this section):

setup()   -- Called once
draw()    -- Called up to 60 times a second
touched() -- Called after draw() each time the viewer is touched

setup()

The setup() function prints an introductory message ("This example tracks multiple touches and colors them based on their ID"). It then creates an empty table touches.

draw()

The draw() function is called by Codea up to 60 times a second, and it does two things: (1) clear the background to opaque black; and (2) draw a circle for each entry in the table touches.

The background is cleared by:

background(0, 0, 0, 255)

An equivalent way of doing the same thing is background(0).

The circles are drawn by:

for k, touch in pairs(touches) do
    -- k is a 'dummy' variable in the sense that its contents are not used in the loop

    -- Use the touch ID as the random seed
    -- This ensures the same random fill color is used for the same ID
    math.randomseed(touch.id)

    -- The strokeWidth is 0, by default
    fill(math.random(255), math.random(255), math.random(255))

    -- Draw ellipse at touch position
    ellipse(touch.x, touch.y, 100, 100)
end

A circle is an ellipse where the width and the height are the same. An equivalent way of doing the same thing is to call the ellipse() function with only three arguments: ellipse(x, y, diameter).

math.random(255) returns a random integer between 1 and 255. The function math.randomseed(seed) sets from where the sequence of 'random' integers will start.

touched(touch)

The touched(touch) function is called by Codea after calling draw() for each touch on the viewer. Information about the touch in question is recorded in the argument touch, which refers to a userdata value.

function touched(touch)
    if touch.state == ENDED then
        touches[touch.id] = nil
    else
        touches[touch.id] = touch
    end
end

Each touch has a unique ID, which can be accessed by using the id index: touch.id. Each touch also has a state, which can be accessed by using the state index: touch.state. The state can be one of three values, which are conveniently represented by three global variables: BEGAN, MOVING or ENDED.

If the touch.state is not ENDED, and the viewer has been touched or the touch is moving on the surface of the viewer, then the touch information is preserved in table touches (using the touch.id as the index).

If the touch.state is ENDED, then the touch information is cleared from the table touches.

Updated