Touch event keeps being missed

I am having a problem with touch events where my code doesn’t always receive the begin and end touch events. This is making it difficult to implement a swipe effect as I can’t cleanly process the beginning and the end of the swipe.

I am listening for touch on a masked image that is sitting below another, transparent image. I wondered if this might be causing a problem but removing the mask and the overlay still shows the same behaviour…

Is this a common problem with touches and if so is there a workaround? [import]uid: 102112 topic_id: 18876 reply_id: 318876[/import]

Posting some code would be helpful.

A run time listener for touch might be helpful. Once a touch is detected you can set focus to the object in question and perform need functions.

Swiping, may make first touch outside the object in question and the touch events are not directed where you want.

-David
[import]uid: 96411 topic_id: 18876 reply_id: 72718[/import]

I have set a touch event listener on the scene parent group and that is performing much more consistently. I am using the code out of the drag/drop sample code folder to handle touches.

I am able to get the simulator to crash pretty regularly when trying to handle touches directly on the object with all the layers enabled so I am wondering if the structure is just too complicated. (a group with a shape and text that is masked in another group with another image on top)

  
local function onTouch( event )  
 local t = event.target  
  
 -- Print info about the event. For actual production code, you should  
 -- not call this function because it wastes CPU resources.  
 printTouch(event)  
  
 local phase = event.phase  
 if "began" == phase then  
 -- Make target the top-most object  
 local parent = t.parent  
 parent:insert( t )  
 display.getCurrentStage():setFocus( t, event.id )  
  
 -- Spurious events can be sent to the target, e.g. the user presses   
 -- elsewhere on the screen and then moves the finger over the target.  
 -- To prevent this, we add this flag. Only when it's true will "move"  
 -- events be sent to the target.  
 t.isFocus = true  
  
 -- Store initial position  
 t.x0 = event.x - t.x  
 t.y0 = event.y - t.y  
 elseif t.isFocus then  
 if "moved" == phase then  
 -- Make object move (we subtract t.x0,t.y0 so that moves are  
 -- relative to initial grab point, rather than object "snapping").  
 t.x = event.x - t.x0  
 t.y = event.y - t.y0  
 elseif "ended" == phase or "cancelled" == phase then  
 display.getCurrentStage():setFocus( t, nil )  
 t.isFocus = false  
 end  
 end  
  
 -- Important to return true. This tells the system that the event  
 -- should not be propagated to listeners of any objects underneath.  
 return true  
end  

[import]uid: 102112 topic_id: 18876 reply_id: 72727[/import]

I do not know if LUA is as Maths (lol) but if so I could say: “The factor`s order does not affect the result.” but if it does not stand out like Maths, try changing your code where:

*you wrote:

[lua]local phase = event.phase
if “began” == phase then[/lua]

*changing just the order:

[lua]local phase = event.phase
if “phase” == “began” then[/lua]

and see if it does change something for you. If so the Math`s principle are into Lua as well. :slight_smile:
Rodrigo.

[import]uid: 89165 topic_id: 18876 reply_id: 72791[/import]

leafcutter: I see exactly the same behavior with touches for a masked image. Occasionally I don’t get the “began” phase. Not good.

Rodrigo: I think you meant phase == "began" not "phase" == "began". In any case leafcutter is using an old programmers trick held over from C to prevent mistakenly typing

phase = "began"  

instead of

phase == "began"  

In C phase = "began" would evaluate the assignment to phase as a boolean instead of checking for equality. So it would compile, just not run right. The assignment "began" = phase would fail to compile since it is not a valid assignment, thus alerting you to your typo.

I’m pretty sure this is not necessary in lua, however, as

if phase = began then  

is not valid lua syntax and will fail to compile. [import]uid: 109175 topic_id: 18876 reply_id: 76713[/import]

@James, Yes I meant that.

Cheers,
[import]uid: 89165 topic_id: 18876 reply_id: 76714[/import]