Detecting a continuous touch

Hi Guys

Can you tell me how to detect a continuous touch?

Eg. I have a listener on an object. I know when the touch began, moved, ended

When the user holds down the thrust button I want to keep adding thrust to my spaceship

Is it simply a case of setting a variable to true when event “began” then setting it to false when event “ended” ?

Yep, that approach would work.  Within the move phase, you might also want to check whether the touch is still within the bounds of your object.  In other words, if the player presses and holds the thrust button, but then slides their finger off the button (but is still holding the screen), you might want the thrust to stop.

  • Andrew

Yes but there is one issue. If you have a touch listener on a display object (not Runtime) then there will be no “ended” phase if you drag your finger off of it. To fix this, on the begin phase you can do:

display.currentStage:setFocus(yourobject)

This will essentially help that object capture all touch events regardless if the touch is actually on it. You can then do some “bounds” checking for the touch when it moves, and if you detect that the touch is not longer on the display objects, just do:

display.currentStage:setFocus(nil)

And then whatever code you have after that you can treat as your “ended” phase

Yes, basically add a true/false flag during the began and ended phases.  But you will also probably need a Runtime:addEventListener(“enterFrame”, doSomething)

handler to continue to add thrust while that value is true.

Thanks guys, your information is really helpful.

:slight_smile:

Would this approach be valid if I was listening for thrust input, in addition to rotateLeft amd rotateRight inputs ? I neglected to mention this initially.

Yep, that approach would work.  Within the move phase, you might also want to check whether the touch is still within the bounds of your object.  In other words, if the player presses and holds the thrust button, but then slides their finger off the button (but is still holding the screen), you might want the thrust to stop.

  • Andrew

Yes but there is one issue. If you have a touch listener on a display object (not Runtime) then there will be no “ended” phase if you drag your finger off of it. To fix this, on the begin phase you can do:

display.currentStage:setFocus(yourobject)

This will essentially help that object capture all touch events regardless if the touch is actually on it. You can then do some “bounds” checking for the touch when it moves, and if you detect that the touch is not longer on the display objects, just do:

display.currentStage:setFocus(nil)

And then whatever code you have after that you can treat as your “ended” phase

Yes, basically add a true/false flag during the began and ended phases.  But you will also probably need a Runtime:addEventListener(“enterFrame”, doSomething)

handler to continue to add thrust while that value is true.

Thanks guys, your information is really helpful.

:slight_smile:

Would this approach be valid if I was listening for thrust input, in addition to rotateLeft amd rotateRight inputs ? I neglected to mention this initially.