Touch question

Hi !
I’m totally new to codea (and to prog globally), so I have a problem which might seem stupid but here it is :
I would like an action to be made if someone touch the screen in a particular spot and to stop when he releases his finger.

At the moment I’m using :

if CurrentTouch.state==MOVING then
  print("pressed")
end

So when I move my finger around it works, but I’d like it to work with a still finger in a particular spot (a button for example)…

If anyone can help me with this, that’d be great !!
Thanks

@Toufalex Here’s a simple example of a button.


function setup()
    str=""
end

function draw()
    background(40, 40, 50)
    fill(255)
    text(str,WIDTH/2,HEIGHT-100)
    rect(200,300,100,50)
    fill(255,0,0)
    text("Press",250,325)   
end

function touched(t)
    if t.state==BEGAN then
        if t.x>200 and t.x<300 and t.y>300 and t.y<350 then
            str="Button pressed"
        end        
    end
    if t.state==ENDED then
        str=""
    end
end

@Toufalex Here’s an example that allows you press or to slide your finger across the button.


function setup()
    str=""
end

function draw()
    background(40, 40, 50)
    fill(255)
    text(str,WIDTH/2,HEIGHT-100)
    rect(200,300,100,50)
    fill(255,0,0)
    text("Press",250,325)   
end

function touched(t)
    if t.state==BEGAN or t.state==MOVING then
        if t.x>200 and t.x<300 and t.y>300 and t.y<350 then
            str="Button pressed"
        else
            str=""
        end      
    end
    if t.state==ENDED then
        str=""
    end
end

Great ! Thanks for the quick answer !
I’ll have a try and let you know if it worked :slight_smile:

Hey !
It worked like a charm ! Thanks

The user and all related content has been deleted.

@NatTheCoder You removed one line of code, and tweaked some text?

The user and all related content has been deleted.