CurrentTouch initial state

CurrentTouch initiates with a random setting, very easy to see if you create a simple project that, in the setup simply prints(CurrentTouch)

Every time you reset the program you’ll get different values for the various bits of currentTouch, and it’ll eventually crash.

Running the latest version of codea (1.5.1)

Best thing to use is function touched(touch) which gets called whenever any sort of touch state occurs

I realise that, but I’m pretty sure it’s still a bug :slight_smile:

Oh well in that case I’m not sure what to tell you, is it only crashing when you’re resetting the game/viewer

Tbh the crashing isn’t the problem, it’s the initial state being random. I’m mostly doing super quick programming examples for my kids (aged 8 and 4) and using CurrentTouch in the draw loop to draw an ellipse around the touch area - as the currentTouch starts of randomly the ellipse gets drawn initially despite there not being a touch (I am using touched(touch) to do things in reaction to the touch, but it was the visual feedback I’m after)

So, here’s a super quick example :

-- touch

-- Use this function to perform your initial setup
function setup()
    taps = 0
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
text(taps,WIDTH/2,HEIGHT/2)
    -- This sets the line thickness
    if CurrentTouch.state~=ENDED then
        ellipse(CurrentTouch.x,CurrentTouch.y,100)
    end
    
end

function touched(touch)
    taps=taps+touch.tapCount
ends

(I mention the crashing only because it happened -it may be totally irrelevant)

Happy to work an alternative (and grateful for any simple suggestions!) but I’m sure I’m right in thinking CurrentTouch should either initiate with nil or other sensible value.

-- ellipse

-- Use this function to perform your initial setup
function setup()
    touch = nil
end

function touched(t)
    --Handling one single touch, stops ellipse jumping between fingers if two or more are on screen
    if t.state == BEGAN and touch == nil then
        touch = t
    elseif touch and t.id == touch.id then
        if t.state == MOVING then
            touch = t
        elseif t.state == ENDED then  
            touch = nil
        end
    end
end
        
-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    if touch then
        strokeWidth(10)
        stroke(135, 62, 29, 255)
        fill(194, 166, 122, 255)
        ellipse(touch.x,touch.y,75)
    end
end

I think this would be a better way of handling it, if this is what you’re looking for, but what you do is fine, you could just do:

If CurrentTouch.state == MOVING then
 ellipse(CurrentTouch.x,CurrentTouch.y,100)
end

Cheers! I came up with a similar solution and will probably just avoid CurrentTouch (or, at least, avoid using it raw!)

-pj