is it possible to pass a touch event to another object?

Hi, 

for a widget.scrollView object there’s a way to pass a touch event to the scrollView, when a button inside the scrollView was dragged with a finger. Sth like this placed in a button’s touch handler:

if(e.phase == "moved") then local dx = abs(e.x - e.xStart) local dy = abs(e.y - e.yStart) -- if finger drags button more than 15 pixels, pass focus to scrollView if(dx \> 15 or dy \> 15) then display.getCurrentStage():setFocus( nil ) e.target = scrollView.\_view e.phase = "began" scrollView.\_view.touch( scrollView.\_view, e ) scrollView:takeFocus(e) elseif(e.phase == "ended") then

I’d like to make sth similar, but with physics objects - so when I drag button which is inside a mapGroup (display.newGroup()) object with a physic body attached - I’d like to pass a touch event from the dragged level button to a mapGroup. I’m trying with this placed in a level button’s touch handler:

if(e.phase == "moved") then local dx = abs(e.x - e.xStart) local dy = abs(e.y - e.yStart) -- if finger drags button more than 15 pixels, pass focus to mapGroup if(dx \> 15 or dy \> 15) then display.getCurrentStage():setFocus( nil ) e.target = mapGroup e.phase = "began" mapGroup.\_view.touch( mapGroup.\_view, e ) display.getCurrentStage():setFocus( e.target ) e.target.isFocus = true end elseif(e.phase == "ended") then

but the line “mapGroup._view.touch( mapGroup._view, e )” doesn’t seem to work with a non-scroll widget object.

Without that line I’m passing just focus to a mapGroup object, but there’s an error due to the lack of touch event data which was not passed.

Does anyone tried to passing a touch event to a display object and could assist me on doing that?

Thanks!

anyone…? :slight_smile:

Are you returning false when you want the event to propogate?

yes, unfortunately it doesn’t change anything. It seems that the level buttons are passing focus to the map object, but apparently already to the “moved” phase, so when it tries to call

body.tempJoint:setTarget(event.x, \_H\*0.5) 

it says that tempJoint field is a nil value…

If your mapGroup is a display group and you have touch table listener registered on it then you should pass focus like this:

        e.target = mapGroup
        e.phase = “began”
        mapGroup.touch( mapGroup, e )
        display.getCurrentStage():setFocus( e.target )

Remeber when you call set focus all the touch events will passed to that object until you clear the focus. Make sure in the ended phase of the mapGroup touch listener you set focus to nil.

If mapGroup.touch( mapGroup, e ) doesn’t give you values for event, that means you probably set up the touch function on mapGroup as a method (mapGroup : touch(event) - with colon). In that case you would have to call it as

mapGroup:touch( e ). Or some other combination. You should post the declaration line if the touch function on mapGroup and how you register the touch listener.

@primoz.cerar, thanks a ton! It works :slight_smile: The thing was that I had a

local dragMap = function(e)

and added its listener to the mapGroup object

Runtime:addEventListener("touch", dragMap)

When I changed it to

function mapGroup:touch(e)

and the added the touch listener - it works perfectly  :slight_smile:

Thank you for your help!

Cheers,

anyone…? :slight_smile:

Are you returning false when you want the event to propogate?

yes, unfortunately it doesn’t change anything. It seems that the level buttons are passing focus to the map object, but apparently already to the “moved” phase, so when it tries to call

body.tempJoint:setTarget(event.x, \_H\*0.5) 

it says that tempJoint field is a nil value…

If your mapGroup is a display group and you have touch table listener registered on it then you should pass focus like this:

        e.target = mapGroup
        e.phase = “began”
        mapGroup.touch( mapGroup, e )
        display.getCurrentStage():setFocus( e.target )

Remeber when you call set focus all the touch events will passed to that object until you clear the focus. Make sure in the ended phase of the mapGroup touch listener you set focus to nil.

If mapGroup.touch( mapGroup, e ) doesn’t give you values for event, that means you probably set up the touch function on mapGroup as a method (mapGroup : touch(event) - with colon). In that case you would have to call it as

mapGroup:touch( e ). Or some other combination. You should post the declaration line if the touch function on mapGroup and how you register the touch listener.

@primoz.cerar, thanks a ton! It works :slight_smile: The thing was that I had a

local dragMap = function(e)

and added its listener to the mapGroup object

Runtime:addEventListener("touch", dragMap)

When I changed it to

function mapGroup:touch(e)

and the added the touch listener - it works perfectly  :slight_smile:

Thank you for your help!

Cheers,