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