Beginner: Touch event end after leaving

I’m just learning Corona at the moment and having trouble with something very basic and unfortunately haven’t been able to find the answer when doing a search. Basically I’ve got a circle on the screen that registers a touch event. When I exit the circle it doesn’t leave the touch event. I did a search an found out I need to set the focus to the object so the event is limited to that object, but still when I leave the object it’s not ending the touch event. Here is the code I have:-

local outterCircle = display.newCircle( 200, 180, 30,10 )  
  
local function listener(event)   
 if event.phase == "began" then  
 event.target.isFocus = true  
 print ("focused")  
 else  
 if event.phase == "ended" or event.phase == "cancelled" then  
 event.target.isFocus = false  
 print ("un-focused")  
 end  
 end  
end   
   
outterCircle:addEventListener("touch", listener)  

If my touch leaves the object I’d like it to end there, but right now it stays active. This is something I’d probably use for controls on the screen, or buttons, or similar things.

If anyone can point me in the right direction it would be greatly appreciated! [import]uid: 133056 topic_id: 23064 reply_id: 323064[/import]

I may be misunderstanding you, but this code runs successfully for me. [import]uid: 21331 topic_id: 23064 reply_id: 92237[/import]

Okay, just to confirm we’re on the same track. I’m using the simulator. I click on the circle, hold the mouse down and move outside of the circle and nothing happens. My goal is once I leave the circle (while still holding the mouse down as a touch event) it should print “un-focused”.

The only way I can get the un-focused event is to release the mouse button (there for ending the touch event) while inside the circle.

That is working correctly for you us it ?

Thanks for your help! [import]uid: 133056 topic_id: 23064 reply_id: 92244[/import]

I believe what you are looking for is “return true” (no "'s) before you end.

Supposedly, it stops propagation of a touch event. I use it in my game, and it works. But sometimes it doesn’t (I’m still trying to work that out haha).

ng [import]uid: 61600 topic_id: 23064 reply_id: 92247[/import]

I believe return true prevents the touch event happening on the remaining objects under the current one. So if you have 3 boxes on top of each other on the screen, and don’t do a return true, the touch event will be triggered on each of them. Is that correct?

I tried adding in a return true (last line before I end the function) and same result unfortunately. Still does not end the touch event when I leave the circle. [import]uid: 133056 topic_id: 23064 reply_id: 92250[/import]

Now that i am clear on the issue…

odd behavior, hugh… I have not done much of this sort of detection…

anyway, this’ll do it…may be other ways to skin the cat, i am not a “touch” expert…yet… :wink:

local Pow = math.pow  
local Sqrt = math.sqrt  
  
local outterCircle = display.newCircle( 200, 180, 30)  
local cir\_radius = outterCircle.width / 2  
  
local function getDistance(x1,y1,x2,y2)  
 local xDist = x2 - x1  
 local yDist = y2 - y1  
 local dist = Sqrt(Pow(xDist,2)+Pow(yDist,2))  
 return dist  
end  
  
local function listener(event)   
 if event.phase == "moved" then  
 local dist = getDistance(outterCircle.x, outterCircle.y, event.x, event.y)  
 if(dist \>= cir\_radius)then  
 print("outside of circle")  
 else  
 print("inside circle")  
 end  
 end  
  
 --return true  
end   
   
Runtime:addEventListener("touch", listener)  

I hope someone pops on and shows us both how to do it with an object listener…but this is what i can do right now.

maybe take a look at:
display.getCurrentStage():setFocus() [import]uid: 21331 topic_id: 23064 reply_id: 92258[/import]

Thanks for that, was going to look at a maths option next. Still love to know how it’d be done though. Found where peach was talking about it at one point

http://developer.anscamobile.com/forum/2011/07/04/whats-isfocus

So looks like I was on the right track with the focus. No idea why it doesn’t work though. Despite having the code there which will work for this, I’ll keep looking to see if I can find.

Looking at the :setFocus() I think thats another way to set the focus to the object. But I’ll dig a little deeper an see what I can come up with!

Thanks again! [import]uid: 133056 topic_id: 23064 reply_id: 92277[/import]

Looks like I was slightly wrong, doing

display.getCurrentStage():setFocus(self, event.id)

will continue to call that event even when you leave that button. You can then use this to check the if you are in the bounds of it or not. So basically if you use TheRealTonyK’s code you can optimisze it by adding the listener to the object rather than runtime.

This is the way UI does it, so guessing it is probably the best way to go about it. [import]uid: 133056 topic_id: 23064 reply_id: 92281[/import]

OK. had some time to look at some of my code and experiment to be sure I knew what it was doing.

local outterCircle = display.newCircle( 200, 180, 30)  
  
local function listener(event)   
 local t = event.target  
  
 if(event.phase == "began")then  
 print("began")  
 display.getCurrentStage():setFocus(t, event.id)  
 t.isFocus = true  
  
 elseif(t.isFocus)then  
 if event.phase == "moved" then  
 print("moved")  
  
 elseif(event.phase == "ended" or event.phase == "cancelled")then  
 print(event.phase)  
 display.getCurrentStage():setFocus(t, nil)  
 t.isFocus = false  
 end  
 end  
end   
   
outterCircle:addEventListener("touch", listener)  

Now the thread will possess the right answer…and a little math :)lol
[import]uid: 21331 topic_id: 23064 reply_id: 92391[/import]

Stevil, I had the same issue, and I solved it like this. I ended up making a touch utils module so I could set up different behaviors and call them as needed. So far I just have this one function in there, but will add more as needed. I may want a case where the button stays on till the touch is actually lifted. There may be better solutions, but this works for me.

MyTouchUtils.lua

[lua]local _M = {}

–buttonTouch()
–To be called upon a button touch event. This is a simple standard button handler.
–A release will be returned if the touch is lifted or slid off the button.
–Arguments: touch event
–Returns:
– 0 = no change
– 1 = button pressed
– 2 = button released
local function buttonTouch(event)
local phase = event.phase
local target = event.target
local bounds = target.contentBounds
if ( “began” == phase ) then
display.getCurrentStage():setFocus(target)
target.isFocus = true
return 1 --pressed
elseif ( target.isFocus ) then
if ( “moved” == phase ) then
–If moved and no longer within button bounds, return release (2).
if ( (event.x < bounds.xMin) or (event.x > bounds.xMax) or
(event.y < bounds.yMin) or (event.y > bounds.yMax) ) then
display.getCurrentStage():setFocus(nil)
target.isFocus = false
return 2 --released
else
return 0 --no change
end
else
–Anything else is a release.
display.getCurrentStage():setFocus(nil)
target.isFocus = false
return 2 --released
end
end
return 0 --no change
end
_M.buttonTouch = buttonTouch

return _M[/lua]
This is how I call it:

[lua]local mtu = require(“MyTouchUtils”)
local function btnTouch(event)
local state = mtu.buttonTouch(event)
–If button pressed
if ( 1 == state ) then
–else if button released
elseif ( 2 == state ) then
end
return true
end

btn:addEventListener(“touch”, btnTouch)[/lua]
[import]uid: 22076 topic_id: 23064 reply_id: 92394[/import]

@Carbondale

clever code [import]uid: 89663 topic_id: 23064 reply_id: 98894[/import]