Type Function Object DisplayObject Library display.* Return value Numbers Revision Release 2024.3703 Keywords display object, coordinates See also object.localToContent object.contentBounds
Maps the given x and y values in content (stage) coordinates to the target object’s local coordinates (center point).
object:contentToLocal( xContent, yContent )
Number. The x coordinate in content (display) space.
Number. The y coordinate in content (display) space.
This function returns the x and y coordinates of the given values in the object’s local coordinate space. The values returned are relative to the center point of the display object, and these values will be negative if the given point is above or left of the object’s center point.
-- Rectangle that fills screen local rect = display.newRect( 0, 0, display.contentWidth, display.contentHeight ) rect.x = display.contentCenterX ; rect.y = display.contentCenterY rect.strokeWidth = 4 ; rect:setStrokeColor( 1, 0, 0 ) rect:setFillColor( 0, 0, 0 ) -- Create text objects to display content and local coordinates local contentText = display.newText( "", 0, 0, display.nativeSystemFont, 16 ) local localText = display.newText( "", 0, 0, display.nativeSystemFont, 16 ) localText.anchorX = 0 ; localText.anchorY = 0 contentText.anchorX = 0 ; contentText.anchorY = 0 function showCoordinates( event ) -- Get x and y of touch event in content coordinates local contentX, contentY = event.x, event.y -- Convert to local coordinates of local localX, localY = event.target:contentToLocal( contentX, contentY ) -- Display content and local coordinate values contentText.text = "CONTENT: " .. tostring(contentX) .. ", " .. tostring(contentY) localText.text = "LOCAL: " .. tostring(localX) .. ", " .. tostring(localY) contentText.x, contentText.y = event.x+20, event.y localText.x, localText.y = event.x+20, event.y+20 localText.anchorX = 0 ; localText.anchorY = 0 contentText.anchorX = 0 ; contentText.anchorY = 0 return true end rect:addEventListener( "touch", showCoordinates )