Actually, setting the default anchor isn’t the answer. Its a possible part of the answer.
In Graphics 2.0 (build numbers > 2000) there were changes to how things are positioned. I’m pretty sure we wrote a blog post about it and the G2.0 conversion guide covered it as well.
For display objects like display.newRect, display.newText, with Graphics 1.0, the numbers you passed in for the X, Y of where to draw the object was treated as the Top, Left of the object. So:
local myText = display.newText( “Hello World!”, 50, 50, Arial, 60 )
Would put the top left corner of the string at 50, 50. However, if you immediately printed the .x and .y values of myText, they would not be 50, 50, the would be the center of the string’s bounding box. This duality of positioning (sometimes x, y means top, left, others it means center and always after the fact it means center) was confusing and inconsistent in the use of the API. Now any time you specify an X, Y when creating an object, it represents the center of the bounding box. Your string is being drawn at 50, 50, but that’s the center of the string, not the top left.
Previously with the Graphics 1 engine, you could position by setting the .x and .y as something other than center by using the object’s :setReferencePoint() setting. We have gotten rid of this option in favor of anchor points. To position an object by it’s top left, you would create the object then immediately set it’s anchor:
local myText = display.newText( “Hello World!”, 50, 50, Arial, 60 )
myText.anchorX = 0.0
myText.anchorY = 0.0
would cause Hello World to now position itself where 50, 50 was the top left of the bounding box.
Now you may need to set a bunch of things, like text strings to use a top left positioning and it’s too much work to always be manually setting the anchor points. We provided a way for you to set the default for future object creation with:
display.setDefault( “anchorX”, 0 )
If you do this, then anything you create after that will be left aligned. This may not be what you want. Circles for instance have always been center positioned. Doing this can break other objects. You should only set the default while you are drawing left aligned objects, then set it back to 0.5 for other things to run correctly.
Rob