what to do when touch target is lost?

Imagine your target for touch events is a 100x100 box.
you then do something when touch begins, moves and ends inside that box.
problem is when you move your finger away from the box, without lifting it.
It seems that the listener function waits in the move phase, and doesn’t execute the end phase.
I tried using event.cancelled, but nothing happens.
I then tried making an exception in the code for when the event.x and event.y exceed the box’s boundaries, not good.
I then tried making an exception in which, if event.target is different than the box, it executes ended phase, same problem, it seems as if it is trying to tun that section of the code everytime the finger approaches near the border of the box.
i didn’t post any code because as you can read i tried several different things, and i believe there must be a way to achieve this that i’m missing.
Thanks! [import]uid: 105206 topic_id: 20883 reply_id: 320883[/import]

Usually the idea is to executed on event.phase == “ended”. For example with a button press you usually do something vaguely like this…

[code]if event.phase == “began” then
– set focus

elseif event.phase == “ended” then

– Check to make sure finger is within the x and y scope of the box

if event.x > event.target.contentBounds.xMin and event.x < event.target.contentBounds.xMax and event.y > event.target.contentBounds.yMin and event.y < event.target.contentBounds.yMax then
– now you know the finger lifted while within the box it started the press on.
– activate whatever the button press was supposed to do
– and probably drop focus afterward
end
end[/code]

If you want to handle what happens when the touch happens *outside* of the box, then you can simply add an else statement there and do it that way. [import]uid: 41884 topic_id: 20883 reply_id: 82265[/import]

Great richard9, thanks! That helped a lot! Just what i was looking for. [import]uid: 105206 topic_id: 20883 reply_id: 82270[/import]

i’m having just one more problem, i’m not setting focus on just one object, they are several objects that together form an irregular figure.
didn’t not quite understand multiple focus in documentation.
for example: i have 3 different sized boxes. they touch each other but they don’t form a rectangle. box1 receives touch event and sets focus on itself, can it also set focus on box2 and box3?
so that all touch events are sent to the area covered by the 3 boxes… [import]uid: 105206 topic_id: 20883 reply_id: 82274[/import]

It sounds like what you need to do is add the objects to a display Group and set focus to the group.

local myGroup = display.newGroup() myGroup:insert(box1) myGroup:insert(box2) myGroup:insert(box3)

From there you can set focus on the group, which means event.target becomes the group object and not any of the boxes. (It also means that your event.target is a big invisible rectangle fitting all of the display objects within it.)

To refer to any of the boxes in the display group, you would use event.target[1], event.target[2] and event.target[3]. So if you absolutely need to know if you touch inside or outside of those three boxes, you need to run a check on each of them:

for i = 1, 3 do -- runs three times if event.x \> event.target[i].contentBounds.xMin and event.x \< event.target[i].contentBounds.xMax and event.y \> event.target[i].contentBounds.yMin and event.y \< event.target[i].contentBounds.yMax then print("It's within box "..i) break else print("It's not within box "..i) end

(‘break’ basically says to stop checking the other boxes if you end inside the first or second box.)

Multiple focus is possible through multi-touch but it’s still one object per touch, I believe. (At least, I haven’t seen any multi-object focus documentation either way…) [import]uid: 41884 topic_id: 20883 reply_id: 82323[/import]

In case it’s helpful for you, more information on touch events here:
http://developer.anscamobile.com/content/events-and-listeners#Touch_Events [import]uid: 52430 topic_id: 20883 reply_id: 82399[/import]

Cool, i didn’t use display groups because i didn’t know that i could access different targets by event.target[x].
This is it, thanks a lot! [import]uid: 105206 topic_id: 20883 reply_id: 82454[/import]

Well i’m having the following problem.
i create a group, then insert objects in it.
then i set focus for that group
now the whole function stopped working
what’s more, when i ask numChildren, it gives me a completely wrong numer.

really, really lost…
[blockcode]
local colorRectsGroup=display.newGroup()
colorRectsGroup:insert(colorTouchRect1)
colorRectsGroup:insert(colorTouchRect2)
colorRectsGroup:insert(colorTouchRect3)
colorRectsGroup:insert(colorTouchRect4)

local function moveBrush( event )
if event.phase==“began” then
display.getCurrentStage():setFocus(colorRectsGroup)
brush.y=event.y
showBrush(event.x, event.y)
local count = display.getCurrentStage().numChildren
print( "Number of objects on the screen = " … count )
print("colorRectsGroup = " … colorRectsGroup.numChildren)
elseif event.phase==“moved” then
brush.x=event.x
brush.y=event.y
–the following is commented because at this moment
–[[for i = 1, 3 do – runs three times
if event.x > event.target[i].contentBounds.xMin and event.x < event.target[i].contentBounds.xMax and event.y > event.target[i].contentBounds.yMin and event.y < event.target[i].contentBounds.yMax then
print("It’s within box "…i)
else
print("It’s not within box "…i)
end
end]]
elseif event.phase==“ended” then
hideBrush()
display.getCurrentStage():setFocus(nil)
end
end
colorTouchRect1:addEventListener(“touch”, pickColor)
colorTouchRect2:addEventListener(“touch”, pickColor)
colorTouchRect3:addEventListener(“touch”, pickColor)
colorTouchRect4:addEventListener(“touch”, pickColor)
colorTouchRect1:addEventListener(“touch”, moveBrush)
colorTouchRect2:addEventListener(“touch”, moveBrush)
colorTouchRect3:addEventListener(“touch”, moveBrush)
colorTouchRect4:addEventListener(“touch”, moveBrush) [import]uid: 105206 topic_id: 20883 reply_id: 82460[/import]

So let’s just walk through this…

  1. you create colorRectsGroup and insert four rectangles
  2. you create the event function ‘moveBrush’
  3. You set event listeners for all four rectangles, although one of the functions is not shown in your code above.

Inside moveBrush(),
– Began –
a. You set focus to the display group
b. You’re moving some kind of brush object to the touch coordinates
– Moved –
a. You’re again moving brush coordinates to touch coordinates
– Ended –
a. You hide the brush object (presumably)
b. You drop focus

So I don’t see anything particularly wrong. I have no experience with assigning multiple listeners to the same object, so could be some oddities there, but what you need to do is this:

  1. Explain which numChildren command is giving the wrong result
  2. Any error messages that appear in the console?
  3. Add print(“I am here #1”) statements to your code. Increment the number as you go. This is vital to figuring out where the code goes and where it stops while debugging. [import]uid: 41884 topic_id: 20883 reply_id: 82467[/import]

forget about pickColor (that func works fine)
both numChildren return wrong results
first one, stage (at this moment colorRectsGroup because of focus, should be 4) returns 7
and second one, colorRectsGroup, which should be the same, returns 0.
No error messages.

Thanks for your help richard. [import]uid: 105206 topic_id: 20883 reply_id: 82500[/import]

I’ve never tried that stage technique so can’t help you there. But since the numChildren command is coming up as zero we can attack there first.

A. What is colorTouchRect1? It needs to be one of these two things to be added to a display group:

[code]-- 1. Anything ‘display.’
local object1 = display.newImageRect(“file.png”, 64, 64) – ok
local object2 = table[1] – probably not

– 2. Any function that returns a display object
local function1 = function()
local myImage = display.newImage(“file.png”)
return myImage – ok!
end

local function2 = function() – won’t work, no display object returned
local myImage = display.newImage(“file.png”)
end[/code]

The other problem is you could be inserting before it exists.

[code] – A good way to fix that is to localize your variables
– Working method
– Even though myVariable is not set initially, so long as it’s declared the function won’t give you problems
local myVariable

local doStuff = function()
myVariable = myVariable + 1
– if you don’t declare ahead of time, this becomes nil = nil + 1
end

myVariable = 23
doStuff()[/code]

It’s hard to say if you’re having either problem because it would be outside of the context of the code snippet you gave/. [import]uid: 41884 topic_id: 20883 reply_id: 82570[/import]

colorRect is a display.newRect object.
It’s not created inside a function and it is inserted into the group after it is created.
i also had declared the variable at the top of my code…

you tell me you have never tried this stage technique, the who would you do it.
I believe it’s a problem about this way of doing it, and maybe there’s something really specific i don’t know i can’t do.

I tell you this because this is the only moment in my code where these things appear, and i believe that what i’m doing is pretty straightforward.
Create 4 rects, add them to a group, focus in that group and then reset focus.

How would you do this?

I really want to thank you for everything! [import]uid: 105206 topic_id: 20883 reply_id: 82710[/import]

Well it definitely sounds like something is interfering or overwriting your group commands. Here’s the code you provided, but now with print statements.

[code]
– Create display group
print("[DEBUG] 1. Adding rectangles to display group")
local colorRectsGroup=display.newGroup()
print("[DEBUG] 1a. group.numChildren = “…colorRectsGroup.numChildren…”, should be 0")
colorRectsGroup:insert(colorTouchRect1)
print("[DEBUG] 1a. group.numChildren = “…colorRectsGroup.numChildren…”, should be 1")
colorRectsGroup:insert(colorTouchRect2)
print("[DEBUG] 1a. group.numChildren = “…colorRectsGroup.numChildren…”, should be 2")
colorRectsGroup:insert(colorTouchRect3)
print("[DEBUG] 1a. group.numChildren = “…colorRectsGroup.numChildren…”, should be 3")
colorRectsGroup:insert(colorTouchRect4)
print("[DEBUG] 1a. group.numChildren = “…colorRectsGroup.numChildren…”, should be 4")

local function moveBrush( event )
if event.phase==“began” then
print("[DEBUG] 3. Moving the brush (began)") display.getCurrentStage():setFocus(colorRectsGroup)
brush.y=event.y
showBrush(event.x, event.y)
local count = display.getCurrentStage().numChildren
print( “stage.numChildren = " … count )
print(“colorRectsGroup.numChildren = " … colorRectsGroup.numChildren)
elseif event.phase==“moved” then
print(”[DEBUG] 4. Moving the brush (moved)”)
brush.x=event.x
brush.y=event.y
–the following is commented because at this moment
–[[for i = 1, 3 do – runs three times
if event.x > event.target[i].contentBounds.xMin and event.x < event.target[i].contentBounds.xMax and event.y > event.target[i].contentBounds.yMin and event.y < event.target[i].contentBounds.yMax then
print("It’s within box "…i)
else
print(“It’s not within box “…i)
end
end]]
elseif event.phase==“ended” then
print(”[DEBUG] 5. Moving the brush (ended)”)
hideBrush()
display.getCurrentStage():setFocus(nil)
end
end

– Add Event Listeners
print("[DEBUG] 2. Adding event listeners")
colorTouchRect1:addEventListener(“touch”, pickColor)
colorTouchRect2:addEventListener(“touch”, pickColor)
colorTouchRect3:addEventListener(“touch”, pickColor)
colorTouchRect4:addEventListener(“touch”, pickColor)
colorTouchRect1:addEventListener(“touch”, moveBrush)
colorTouchRect2:addEventListener(“touch”, moveBrush)
colorTouchRect3:addEventListener(“touch”, moveBrush)
colorTouchRect4:addEventListener(“touch”, moveBrush)[/code]
Use these statements to figure out what *is* happening and where stuff *stops* happening, and report back here :slight_smile: [import]uid: 41884 topic_id: 20883 reply_id: 82740[/import]