#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Two touches = one moving?

edited July 2014 in General Posts: 8

When I touch one area then touch another (2 touches at once)codea recognizes it as one single touch moving, meaning that if the first or second touch is lifted, both are recognized as ended. Help?

Tagged:

• edited July 2014 Posts: 8,739

@Mac_n_chese Look at this code to see how multiple touches can be kept independent of each other. Touch the screen with several fingers and move them. Lift your finger and retouch to see how the touched are seperate from each other.

``````displayMode(FULLSCREEN)

function setup()
tab={}  -- table for touched info, (x,y,id,color)
col={}  -- table for 10 colors
for z=1,10 do   -- create 10 random colors
c=color(math.random(255),math.random(255),math.random(255))
table.insert(col,c)
end
end

function draw()
background(0, 0, 0, 255)
for a,b in pairs(tab) do    -- use touch info to draw colored circles
fill(col[b.w])  -- set color of touched circle
ellipse(b.x,b.y, 100, 100)  -- draw circle at touched x,y location
end
end

function touched(t)
if t.state==BEGAN then  -- touch started
for a,b in pairs(tab) do    -- check if touched id is already in table
if t.id==b.z then   -- id in table, exit touched function
return
end
end
-- touch id isn't in the table
-- save x,y values, touch id, and a color from 1,10
table.insert(tab,vec4(t.x,t.y,t.id,math.random(10)))
end
if t.state==MOVING then -- check which touch id is moving
for a,b in pairs(tab) do
if t.id==b.z then   -- id matches table
b.x=t.x -- update x,y values with new location
b.y=t.y
end
end
end
if t.state==ENDED then  -- check which touch id has ended
for a,b in pairs(tab) do
if t.id==b.z then   -- touch id matches table
table.remove(tab,a) -- remove it from the table
end
end
end
end

``````
• Posts: 8

You never cease to amaze me with your quick and helpful replies @dave1707
I'll look into it • Posts: 8,739

@Mac_n_chese I don't have any comments on the code, so if you have any questions, I can add some.

• edited July 2014 Posts: 8

I'm understanding most of it, but if I have any questions I'll ask. Thanks • Posts: 8

• Posts: 8,739

• Posts: 8

I do have one question..... In the multitouch example ( which I found after posting this discussion) uses something like: tab[t.id]=t. What is it in this example that does the same thing?

• Posts: 8,739

@Mac_n_chese tab[t.id]=t is just creating a table using the id as the key to a table. What I'm doing is saving the x,y,id,color values as a vec4 in a table. So the uses of the tables are different.

• Posts: 8

Ah I see, thanks again