Multitouch Fingers

Features demonstrated:

How to use create draggable objects. Also shows how to move an object to the top.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
system.activate( "multitouch" )
 
local arguments =
{
        { x=50, y=10, w=100, h=100, r=10, red=255, green=0, blue=128 },
        { x=10, y=50, w=100, h=100, r=10, red=0, green=128, blue=255 },
        { x=90, y=90, w=100, h=100, r=10, red=255, green=255, blue=0 }
}
 
local function printTouch( event )
        if event.target then 
                local bounds = event.target.stageBounds
                print( "event(" .. event.phase .. ") ("..event.x..","..event.y..") bounds: "..bounds.xMin..","..bounds.yMin..","..bounds.xMax..","..bounds.yMax )
        end 
end
 
 
-- Display the Event info on the screen
local function showEvent( event )
        txtPhase.text = "Phase: " .. event.phase
        txtXY.text = "(" .. event.x .. "," .. event.y .. ")"
        txtId.text = "Id: " .. tostring( event.id )
end
 
local function onTouch( event )
        local t = event.target
        showEvent( event )
        
        print ("onTouch - event: " .. tostring(event.target), event.phase, event.target.x, tostring(event.id) )
 
        -- Print info about the event. For actual production code, you should
        -- not call this function because it wastes CPU resources.
        printTouch(event)
 
        local phase = event.phase
        if "began" == phase then
                -- Make target the top-most object
                local parent = t.parent
                parent:insert( t )
                display.getCurrentStage():setFocus( t, event.id )
 
                -- **tjn: I don't this this comment applies in Beta 6 Multitouch
                -- I also don't think we need t.isFocus.
                --
                -- Spurious events can be sent to the target, e.g. the user presses 
                -- elsewhere on the screen and then moves the finger over the target.
                -- To prevent this, we add this flag. Only when it's true will "move"
                -- events be sent to the target.
                t.isFocus = true
 
                -- Store initial position
                t.x0 = event.x - t.x
                t.y0 = event.y - t.y
        elseif t.isFocus then
                if "moved" == phase then
                        -- Make object move (we subtract t.x0,t.y0 so that moves are
                        -- relative to initial grab point, rather than object "snapping").
                        t.x = event.x - t.x0
                        t.y = event.y - t.y0
                elseif "ended" == phase or "cancelled" == phase then
                        if not(nil == t.id) then
--                              print("Removing object from stage")
                                t:removeSelf()
                        else
                                display.getCurrentStage():setFocus( t, nil )
                                t.isFocus = false
                        end
                end
        end
 
        -- Important to return true. This tells the system that the event
        -- should not be propagated to listeners of any objects underneath.
        return true
end
 
-- Iterate through arguments array and create rounded rects (vector objects) for each item
for _,item in ipairs( arguments ) do
        local button = display.newRoundedRect( item.x, item.y, item.w, item.h, item.r )
        button:setFillColor( item.red, item.green, item.blue )
        button.strokeWidth = 6
        button:setStrokeColor( 200,200,200,255 )
 
        -- Make the button instance respond to touch events
        button:addEventListener( "touch", onTouch )
end
 
-- listener used by Runtime object. This gets called if no other display object
-- intercepts the event.
--
-- Create a circle where the user touched the screen.
-- Multiple touches will generate multiple circles
-- Moving the object is handled in onTouch()
local function otherTouch( event )
        
        print( "otherTouch: event(" .. event.phase .. ") ("..event.x..","..event.y..")" .. tostring(event.id) )
        local s = display.getCurrentStage() 
                                        
        if "began" == event.phase then
                showEvent( event )
 
--              print("Found otherTouch -- began", display.getCurrentStage())
                local circle = display.newCircle(event.x, event.y, 45)
                circle:setFillColor(  0, 0, 255, 255 )          -- start with Alpha = 0
                
                -- Store initial position
                circle.x0 = event.x - circle.x
                circle.y0 = event.y - circle.y
                circle.isFocus = true           -- **tbr - remove later
                touchCircle = circle            -- **tbr - temp
                circle.id = event.id            -- save our id so we can access the object later
--              print( "new circle:", tostring(circle) ); print()
 
                circle:addEventListener( "touch", onTouch )
                event.target = circle
                onTouch( event )
        end     
end
 
-- Define text areas on the screen
txtPhase = display.newText( "Phase: _____", 10, 440, "Verdana-Bold", 12 )
txtPhase:setTextColor( 255,255,255 )
 
txtXY = display.newText( "(___,___)", 10, 455, "Verdana-Bold", 12 )
txtXY:setTextColor( 255,255,255 )
 
txtId = display.newText( "Id: ______", 190, 440, "Verdana-Bold", 12 )
txtId:setTextColor( 255,255,255 )
 
Runtime:addEventListener( "touch", otherTouch )
--      Runtime:addEventListener( "touch", printTouch2 )        -- **tjn No longer used