Question

I am trying to understand those reference points in Corona. It is said that the default reference point is the center of the object, however the x position of center of the textObject isn't at 100 but 150 when I run this code:

  local textObject = display.newText(  "Hello World!", 100, 100, nil , 20 )

I displayed those position with touch event on the screen and printing the x and y position of touched places:

  local function onScreenTouch(event)
      print("x: "..event.x.."  y: "..event.y);
  end
  Runtime:addEventListener("tap", onScreenTouch)
Was it helpful?

Solution

Very good question and it's something that drove me nuts for a long time.

By default the reference point is display.CenterReferencePoint unless you are passing the X, Y as parameters to an API call. In most cases (for rectangles), passing an X, Y becomes Top Left reference point only for the initial draw of the object. Lines of course you're passing end points and for circles your X, Y is the center.

myRect = display.rect(X, Y, width, height)  -- X, Y = Top, Left

in that case, X and Y represent the Top Left corner of the box however afterwards if you do:

myRect.x = X -- X = center of object
myRect.y = Y -- Y = center of object

then you are now using a center reference point unless you explicitly change it back to something else. Now because this is confusing, I tend to do this:

myRect = display.rect(0,0, width, height)
myRect.x = 100
myRect.y = 100

just as a way that I'm really only positioning things using the center point.

Hope that explains it.

Rob

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top