Touch - is this expected?

All,

I was playing with a little text routine involving printing every time a touch is made. What I found was the routine printed at least twice. I find this odd as I restricted the print to when touch is ENDED.

Routine below


-- TestTouch

function setup()
    --
    count = 0
end

function draw()
    -- 
    background(40, 40, 50)
end

function touched(touch)
    --
    if ENDED then
        count = count + 1
    end
    print(count)
end


Further,

Tried to see if this was me being a bit naive and used the following code,


function setup()
    --
    count = 0
end

function draw()
    -- 
    background(40, 40, 50)
end

function touched(touch)
    --
    if touch == ENDED then
        count = count + 1
    end
    print("count = "..count )
end

Still the same issue but in addition - the count printing was summed up so you just got a single line with a summed number. I know this is part of the print routine but the actual variable count was not incremented in the printout. Could there be some way to specify accumulation on/off so we can see the detail if needed?

@Bri_G You need to put the print statement inside the if statement. Right now it prints when you touch the screen (BEGAN) and then when you untouch the screen (ENDED). If you slide your finger on the screen (CHANGED) it will print for each change. The touch function always works for each thing, you need the if statements to specify which one you want to use.

@dave1707 - being a bit thick here but - count should only be incremented when touch is ENDED not BEGAN.

Also, since the print statement changes each time you touch there should be multiple lines with new values in.

Ages since I’ve used this in this way, I tend to build up single print strings with linefeeds to display the changing data. May have to resort to that here.

@Bri_G My mistake, I just got up and wasn’t thinking yet. It should be touch.state in the if statement.

@dave1707 - doohhh, thanks for that. Fallen foul of that a few times.