Problem with touchfunction

hey guys I’m working on an project, where you can write a text in a box and if you tap on a button the text would seaked out. But now I’ve got a problem with my code ( touchfunction ). I think the touchfuction of my project is a endless slope, so if you press the button, so if you press the button the speakword() function works more then one time and speak the text two or tree … Times

So here is my code :slight_smile: ::

function touched(touch)
if CurrentTouch.x > 0 and CurrentTouch.x < 768 and CurrentTouch.y > 380 and CurrentTouch.y < 880 then
        
            showKeyboard()
        
        elseif
    CurrentTouch.x > 259 and CurrentTouch.x < 509 and CurrentTouch.y > 150 and CurrentTouch.y < 250 then
        
            speakWords()
    
        elseif
    CurrentTouch.x > 700 and CurrentTouch.x < 780 and CurrentTouch.y > 874 and CurrentTouch.y < 946 then
        
        words = ""
        
        elseif
    CurrentTouch.x > 610 and CurrentTouch.x < 690 and CurrentTouch.y > 870 and CurrentTouch.y < 950 then
        
        words = pasteboard.text
    end

end

It would be nice if somebody of you could help me with my problem :slight_smile: that would be very nice
Thank you

Whenever you tap the screen, the touched function runs twice. Once when the touch starts, and once when it ends. I fixed this by adding a statement check whether the state is ENDED (when the finger leaves the screen). Also, in the touched function, it’s better to use touch rather than CurrentTouch.

function touched(touch)
    if touch.state==ENDED then
        if touch.x > 0 and touch.x < 768 and touch.y > 380 and touch.y < 880 then
            showKeyboard()
        elseif
        touch.x > 259 and touch.x < 509 and touch.y > 150 and touch.y < 250 then
            speakWords()
        elseif
        touch.x > 700 and touch.x < 780 and touch.y > 874 and touch.y < 946 then
            words = ""
        elseif
        touch.x > 610 and touch.x < 690 and touch.y > 870 and touch.y < 950 then          
            words = pasteboard.text
        end
    end
end

Wow thank you very much for your fast and very helpful answer :slight_smile: it works very very good :smiley: Tank you

I also find it useful to place touch functions within an if statement to create menus at different stages of my game or app.

E.g.

function touched (touch)
  if gamestate == menu then
    if touch.state etc.....
  elseif gamestate == play then
    if touch.state etc...
  elseif gamestate == gameover then
    if touch.state etc...
  end
end