Is it possible to make something only happen once in the draw loop?

Hello all, sorry if the answer to this question is obvious, but I’m still new to programming and so I am a bit stuck on this:

Let’s say I want the program to execute a function every time the screen is tapped.


function draw()

if CurrentTouch.state == BEGAN then

docode()

end

end

The problem with this however is that since it is in draw(), the function will be called something like 4-5 times before the state changes from “BEGAN”.

To solve this problem I usually do something like:


function setup()
stop_docode_repeating = 0
end

function draw()

if CurrentTouch.state == BEGAN and stop_docode_repeating == 0 then

docode()
stop_docode_repeating = stop_docode_repeating + 1

end

end

Although this works, it is quite clumsy and quite slow to do. Is there a better way of doing this?

Thanks!

Use the touched function.

function touched(touch)
    if touch.state == BEGAN then
        docode()
    end
end

Worked like a charm, thank you very much!