Any comments on my touchEventHelper?

Also a nice and dynamic approach! I also considered giving the objects itself the “dynamic options”, but in my situation it is preferable to have the “dynamic options” in the touchEventObject itself.

1 of the reasons is that in the GenericTouchHandler approach I need to pass the parameters to every object that has a touchEventListener, in the touchEventHelper approach I only need to pass it to the touchEventObject and can re-use the touchEventObject for multiple objects

2nd reason is that im also using the touchEventObject to simulate touches with my gamepad.

onTypeIconClick({ phase = "ended", simulateTouch = true, target = cursorTarget })

Altogether, I think this topic is a good source to find inspiration to create a touch event helper for different situations :slight_smile:

fwiw re 1) - the option table passed could be “shared” by all objects sharing same values, (that is, the options only need be unique when they ARE unique, much as per your original code), fe:

local sharedOptions = { onMove=function() end, etc... } GenericTouchHandler:addListener( object1, sharedOptions ) GenericTouchHandler:addListener( object2, sharedOptions ) GenericTouchHandler:addListener( object3, sharedOptions ) local justObject4Options = { onMove=function() end, etc...} GenericTouchHandler:addListener( object4, justObject4Options )  

Going to chime in: My quite-simple-but-does-the-job approach is that I have a single library, “touch.lua”, whose only function is makeObjectTouchSensitive(). It’s a robust touch listener with inside bounds, outside bounds, multitouch, etc., which calls the callbacks according to each event. So in the end, my code looks like this:

touch.makeObjectTouchSensitive(obj) obj.touchCallbacks.endedOutsideBounds = function(event) print("Cancelled.") end obj.touchCallbacks.endedInsideBounds = function(event) print("Do something!") end obj:setTouchEnabled(true) -- Allows/disallows touch, calling the cancel events if need be

Just something else to think about. It’s simple but it works really well.

  • Caleb