Ignoring input from more than one finger?

I’m having trouble figuring out how the multitouch class works, and even more trouble trying to get it to read only one touch on the screen and not any more. I’ve added the relevant code below, and would appreciate if someone would help me on how to ignore inputs from other fingers, so that the circle follows only one finger and not any others.

I also tried to edit the code myself, but it didn’t help.



function setup()
    touches = {}
    taps = 0
end

-- This function gets called whenever a touch
--  begins or changes state
function touched(touch)
    
    if touch.state == ENDED then
        -- When any touch ends, remove it from
        --  our table
        taps = 0
        touches[touch.id] = nil
        else
        -- If the touch is in any other state
        --  (such as BEGAN) we add it to our
        --  table
        
        touches[touch.id] = touch
        if taps >= 2 then
        touches[touch.id] = nil
        end
        
        if touch.state == BEGAN then
            taps = taps + 1
        end
    end
end

function draw()
    background(0, 0, 0, 255)
    print(taps)
    for k,touch in pairs(touches) do
        
        -- Draw ellipse at touch position
        ellipse(touch.x, touch.y, 100, 100)
        
    end
end

@YoloSwag I’m not sure why you have a table for touches if you only want a one touch object. Here’s an example of a one touch object ignoring other touches.


function setup()
    id=0
end

function draw()
    background(0, 0, 0, 255)
    fill(255)
    if tx~=nil then
        ellipse(tx,ty, 100, 100)
    end
end

function touched(t)
    if t.state==BEGAN and id==0 then
        id=t.id
    end            
    if t.state==MOVING and t.id==id then
        tx=t.x
        ty=t.y
    end
    if t.state==ENDED and t.id==id then
        id=0
        tx=nil
    end
end

@YoloSwag If you want each touch to control a different circle, try this. I added color to the circles.


displayMode(FULLSCREEN)

function setup()
    tab={}
    col={}
    for z=1,10 do
        c=color(math.random(255),math.random(255),math.random(255))
        table.insert(col,c)
    end
    id=0
end

function draw()
    background(0, 0, 0, 255)
    for a,b in pairs(tab) do
        fill(col[b.w])
        ellipse(b.x,b.y, 100, 100)
    end
end

function touched(t)
    if t.state==BEGAN then
        for a,b in pairs(tab) do
            if t.id==b.z then
                return
            end
        end
        table.insert(tab,vec4(t.x,t.y,t.id,math.random(10)))
    end            
    if t.state==MOVING then
        for a,b in pairs(tab) do
            if t.id==b.z then
                b.x=t.x
                b.y=t.y
            end
        end
    end
    if t.state==ENDED then
        for a,b in pairs(tab) do
            if t.id==b.z then
                table.remove(tab,a)
            end
        end
    end
end

Thanks @dave1707!