question about touch

Hi

I have a text object with a touch listener attached. When the touch starts, the text turns red. When the touch ends, the text object is removed.

This works provided my finger is touching the object when I lift it off. If I touch the object then slide my finger so it’s no longer on the object, and THEN remove it, the app doesn’t register the end of the touch and then I have to touch again.

As I write this I’m thinking this is probably exactly the expected behaviour, but is there anything I can do to get the app to detect the departing finger if it’s no longer on the text object?

thanks in advance,

David

Yes there is!

When you begin the touch, you need to tell the touch event to keep it’s focus on the object until you tell it otherwise using the function “getFocus()”:

local function myTouch(event) if event.phase == "began" then --here we use the setFocus() function to keep the --touch event focused on the object display.getCurrentStage():setFocus(event.target) --turn the object red event.target:setFillColor(1, 0, 0) elseif event.phase == "moved" then --do any move stuff you want elseif event.phase == "ended" then --once the touch ends we remove the focus from the object --which allows us to touch another object again display.getCurrentStage():setFocus(nil) --remove the object event.target:removeSelf() event.target = nil end end local myText = blah blah blah myText:addEventListener("touch", myTouch)

If you are using multitouch, you also need to pass in another property which is the touch ID (so that each touch can be focused on a different object). It’s all here is the docs page:

http://docs.coronalabs.com/api/type/StageObject/setFocus.html

Hi

THanks for the reply. I didn’t know about this, and it helped a lot. It didn’t work at first but I changed  

elseif event.touch == “ended” then

to

elseif event.phase == “ended” then

and then it worked.

thanks!

Oops you’re quite correct, it was a typo. I’ve updated my original post.

Yes there is!

When you begin the touch, you need to tell the touch event to keep it’s focus on the object until you tell it otherwise using the function “getFocus()”:

local function myTouch(event) if event.phase == "began" then --here we use the setFocus() function to keep the --touch event focused on the object display.getCurrentStage():setFocus(event.target) --turn the object red event.target:setFillColor(1, 0, 0) elseif event.phase == "moved" then --do any move stuff you want elseif event.phase == "ended" then --once the touch ends we remove the focus from the object --which allows us to touch another object again display.getCurrentStage():setFocus(nil) --remove the object event.target:removeSelf() event.target = nil end end local myText = blah blah blah myText:addEventListener("touch", myTouch)

If you are using multitouch, you also need to pass in another property which is the touch ID (so that each touch can be focused on a different object). It’s all here is the docs page:

http://docs.coronalabs.com/api/type/StageObject/setFocus.html

Hi

THanks for the reply. I didn’t know about this, and it helped a lot. It didn’t work at first but I changed  

elseif event.touch == “ended” then

to

elseif event.phase == “ended” then

and then it worked.

thanks!

Oops you’re quite correct, it was a typo. I’ve updated my original post.