[SOLVED] Pics responding to tapping

Hi all!
I’m new in the Lua and Codea, and I wanna ask if there is a way to interact with the pics. Such as -

if pic == touched then
stroke(255,0,0,255)
x = 90
end

I need to know what the “if pic == touched then” part should be.

Thank you

  • JustPlaying

@JustPlaying You need to know the edge x,y values of the picture. The x,y edge values will vary based on the size of the picture and where on the screen you place it. Then it’s just a matter of where you touch.

if touch.x > left_edge and touch.x < right_edge and touch.y > bottom_edge and touch.y < top_edge then do something. 

@JustPlaying Here’s an example using a sprite. The sprite is 110x110 pixels in size which is why I add and subtract 55 ( 110 / 2 ) to get the edge values from where the sprite is displayed. Just touch the sprite or slide your finger across it.


function setup()
end

function draw()
    background(40, 40, 50)  
    fill(255) 
    sprite("Planet Cute:Icon",WIDTH/2,HEIGHT/2) 
    if hit then
        text("sprite touched",WIDTH/2,HEIGHT/2+200)
    end
end

function touched(t)
    if t.state==BEGAN or t.state==MOVING then
        if t.x>WIDTH/2-55 and t.x<WIDTH/2+55 and 
            t.y>HEIGHT/2-55 and t.y<HEIGHT/2+55 then
                hit=true
        else
            hit=false
        end
    end 
    if t.state==ENDED then
        hit=false
    end  
end
    

Thank you :smiley: