Global Touch

Hi,
I know it might be simple and I am missing the obvious, but how do I set a listener on the main screen for touches irrespective of what element was touched, a kind of a parent touch.

thanks and cheers,

Jayant C Varma [import]uid: 3826 topic_id: 3050 reply_id: 303050[/import]

I was looking for that last night and ended up setting a listener on the background image I was using, which worked okay.

Is there a better way to handle a “non-specific object” touch?

Jay
[import]uid: 9440 topic_id: 3050 reply_id: 8992[/import]

Hi jay,
Thanks that worked for me, I placed a background and set a listener on it, then I tried the Runtime listener, and that works too, so I do not have to have a background object. I knew it was obvious, set it on the root UIView (oops) on the Runtime and it works.

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 3050 reply_id: 8999[/import]

Here you go. No need to use a background image.

[lua]function onTouch(event)
if event.phase ==“began” then
local xpos = event.x
local ypos = event.y
print (“X:”…xpos…" : "…“Y:”…ypos)
end
end

Runtime:addEventListener(“touch”, onTouch)[/lua] [import]uid: 5712 topic_id: 3050 reply_id: 9008[/import]

thanks Mike,
i figured that and if you read my last message, I used Runtime events, but thanks for the code, if any new comer would search for this, there is sample code.

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 3050 reply_id: 9010[/import]

Sorry, didn’t read your post correctly. [import]uid: 5712 topic_id: 3050 reply_id: 9072[/import]

I’ve had success by adding a non-Runtime listener to the game’s “stage”, as follows:

local stageRef = display.getCurrentStage()  
  
function stageRef:tap( event )  
-- do something  
return true  
end  
  
stageRef:addEventListener( "tap", stageRef )  

Remember to place other objects above the stageRef in z-index order, so they take precedence over the stageRef tap (i.e. if you have other objects you want to manipulate, they must take precedence over the stage in terms of tapping on them)
[import]uid: 9747 topic_id: 3050 reply_id: 9073[/import]