How get global X/Y coordinates from Tap event ?

When I calling Tap event, I can get event.x and event.y inside event function. But it’s only screen coordinates, not absolute.

How to get absolute world coordinates x/y of pixel, where I tapping ?

Best Regards,

PS: I recorded a video: http://snap.ashampoo.com/fPVZCZ6n

See if this will help:

http://docs.coronalabs.com/api/type/DisplayObject/localToContent.html

Rob

Thanks for the answer, but nothing happened. I created a simple example of my problem. If someone run this code and help me, I’ll will be very happy.

Thanks.

Example code:

local world = display.newGroup( ) function scroll(event) if event.phase == "began" then world.xStart = world.x world.yStart = world.y world.xBegan = event.x world.yBegan = event.y elseif ( "moved" == event.phase ) then local dx = event.x - world.xBegan local dy = event.y - world.yBegan local x = dx + world.xStart local y = dy + world.yStart world.x = x world.y = y end end function tap(event) print ("X: ".. event.x .. " Y:" .. event.y) -- How return X and Y coordinates here ? not local Coordinates in screen size end -- Load Any image, example only working with big images local background = display.newImageRect( "Icon-60@3x.png", 4098, 3096 ) world:insert (background) display.currentStage:addEventListener( "touch", scroll ) Runtime:addEventListener( "tap", tap )

Wow, I made it  :smiley:

display.setDefault( "anchorX", 0 ) display.setDefault( "anchorY", 0 ) local world = display.newGroup( ) function scroll(event) if event.phase == "began" then world.xStart = world.x world.yStart = world.y world.xBegan = event.x world.yBegan = event.y elseif ( "moved" == event.phase ) then local dx = event.x - world.xBegan local dy = event.y - world.yBegan local x = dx + world.xStart local y = dy + world.yStart world.x = x world.y = y end end local background = display.newImageRect( "Icon-60@3x.png", 4098, 3096 ) world:insert (background) function tap(event) local square = display.newRect( event.x-world.x, event.y-world.y, 2, 2) square:setFillColor( 1 ) world:insert (square) local sqCenterX, sqCenterY = square:localToContent( -world.x, -world.y ) print( "White square's center position in screen coordinates: ", sqCenterX, sqCenterY ) end display.currentStage:addEventListener( "touch", scroll ) Runtime:addEventListener( "tap", tap )
  1. First of all, you need set defaultAnchorX and Y to 0. In centralization system I was be more confused. But you can make it without nulling defaultAnchor, just divide in half
  2. Create new object in tap function end set coordinates: event.x - world.x, event.y - world.y. World it’s our group with objects which scrolled. Then add helper object to this group
  3. Then, use method localToContent with this ccordinates: -world.x, -world.y

See if this will help:

http://docs.coronalabs.com/api/type/DisplayObject/localToContent.html

Rob

Thanks for the answer, but nothing happened. I created a simple example of my problem. If someone run this code and help me, I’ll will be very happy.

Thanks.

Example code:

local world = display.newGroup( ) function scroll(event) if event.phase == "began" then world.xStart = world.x world.yStart = world.y world.xBegan = event.x world.yBegan = event.y elseif ( "moved" == event.phase ) then local dx = event.x - world.xBegan local dy = event.y - world.yBegan local x = dx + world.xStart local y = dy + world.yStart world.x = x world.y = y end end function tap(event) print ("X: ".. event.x .. " Y:" .. event.y) -- How return X and Y coordinates here ? not local Coordinates in screen size end -- Load Any image, example only working with big images local background = display.newImageRect( "Icon-60@3x.png", 4098, 3096 ) world:insert (background) display.currentStage:addEventListener( "touch", scroll ) Runtime:addEventListener( "tap", tap )

Wow, I made it  :smiley:

display.setDefault( "anchorX", 0 ) display.setDefault( "anchorY", 0 ) local world = display.newGroup( ) function scroll(event) if event.phase == "began" then world.xStart = world.x world.yStart = world.y world.xBegan = event.x world.yBegan = event.y elseif ( "moved" == event.phase ) then local dx = event.x - world.xBegan local dy = event.y - world.yBegan local x = dx + world.xStart local y = dy + world.yStart world.x = x world.y = y end end local background = display.newImageRect( "Icon-60@3x.png", 4098, 3096 ) world:insert (background) function tap(event) local square = display.newRect( event.x-world.x, event.y-world.y, 2, 2) square:setFillColor( 1 ) world:insert (square) local sqCenterX, sqCenterY = square:localToContent( -world.x, -world.y ) print( "White square's center position in screen coordinates: ", sqCenterX, sqCenterY ) end display.currentStage:addEventListener( "touch", scroll ) Runtime:addEventListener( "tap", tap )
  1. First of all, you need set defaultAnchorX and Y to 0. In centralization system I was be more confused. But you can make it without nulling defaultAnchor, just divide in half
  2. Create new object in tap function end set coordinates: event.x - world.x, event.y - world.y. World it’s our group with objects which scrolled. Then add helper object to this group
  3. Then, use method localToContent with this ccordinates: -world.x, -world.y