Touches

I’m wondering how I could detect touches without using a ton of condtional code. Could sombody help ;:wink: ? Thanks .

To do what? It depends on what you need.

I don’t know, for example when you tap a circle the screen turns red. The only way i know to do this is

-- Circle

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
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
    ellipse(WIDTH/2,HEIGHT/2,200)
    
end
function touched(touch)
    if touch.state == ENDED and touch.x< --blah blah

end

@Ignatz

that looks fine to me. You seem to need those conditions in the touch function. What is the problem?

I’m justt wondering if there is an easier way to detect touches than to take 10 lines to test for each touch.
@Ignatz

where are the 10 lines?

if the position of your ellipse is at e=vec2(WIDTH/2,HEIGHT/2), with diameter eWidth=200, and the colour of your ellipse is stored in colr, then you can write

if touch.state==ENDED and e:dist(vec2(touch.x,touch.y))<eWidth/2 then 
    colr=color(255,0,0) --change ellipse colour to red
end

@infiltration265 Here’s something I had, maybe this will help.

supportedOrientations(PORTRAIT)
displayMode(FULLSCREEN)

function setup()
    count=50   
    circ={}  -- table for x,y coordinates
    colr={}  -- table for color  
    for x=1,count do
        circ[x]=vec2(math.random(50,WIDTH-50),math.random(50,HEIGHT-50))
        colr[x]=vec3(255,255,255)
    end
end

function draw()
    background(40, 40, 50)    
    x=CurrentTouch.x    -- touched x value
    y=CurrentTouch.y    -- touched y value     
    for x1=1,count do
        xc=circ[x1].x  -- get x,y coordinates
        yc=circ[x1].y        
        c=colr[x1]  -- get color
        c=fill(color(c.x,c.y,c.z))
        ellipse(xc,yc,50,50)        
        -- check if object is touched
        if (y-yc)^2/20^2+(x-xc)^2/20^2 <= 1 then
            colr[x1]=vec3(255,0,0)  -- change color to red
        end  
    end
end

@Ignatz Thanks!!! It worked perfectly. I thought the only way to detect a touch on a certain object was to do something like

if touch.state == ENDED and touch.y > ellipse1y-ellipsesizeh/2 and touch.y < ellipse1y+ellipsesizeh and touch.x > ellipsex-ellipsesizew/2 and touch.x < ellipsex+ellipsesizew/2 then

@infiltration265 If you’re checking for a box, you can also do:

if math.abs(touch.x - rectx) <= rectwidth / 2 and math.abs(touch.y - recty) <= rectheight / 2 then

@infiltration265 - check out the other vec functions in the built in docs, too. There are some useful things there.