Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

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

edited May 2014 in Questions Posts: 8

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?



  • Posts: 2,042

    Use the touched function.

    function touched(touch)
        if touch.state == BEGAN then
  • Posts: 8

    Worked like a charm, thank you very much!

Sign In or Register to comment.