Simple question! on placing Rect in the center of the screen

I guess, the tutorial I’m watching now might be out-of-date. Because there’s a guy typing this code:

– most commonly used screen coordinates

centerX = display.contentCenterX

centerY = display.contentCenterY

screenLeft = display.screenOriginX

screenWidth = display.contentWidth - screenLeft * 2

screenRight = screenLeft + screenWidth

screenTop = display.screenOriginY

screenHeight = display.contentHeight - screenTop * 2

screenBottom = screenTop + screenHeight

display.contentWidth = screenWidth

display.contentHeight = screenHeight

– create a background

local sky = display.newRect( screenLeft, screenTop, screenWidth, screenHeight )

sky:setFillColor ( 0, 25, 111 )

And he gets the Rect in the center! I double checked everything and still getting the Rect fitted between the left upper conner and the center of the screen. Could you please explain why this happens? Thank you! 

I see you got an answer to this on the community slack, but so the answer will be searchable and usable by others.

Many years ago (pre-2013) there were some inconsistencies in our API’s. for instance display.newImage() would center the object as would display.newCircle() and other API’s like display.newRect() would position using the top, left corner. When we released Graphics 2.0 in 2013, we made all of our API’s consistent, and now they all default to the center of the object for positioning.

You can either use the center of the object or if you want to use the top, left corner, you can use anchor points:

local sky = display.newRect( screenLeft, screenTop, screenWidth, screenHeight) sky.anchorX = 0 sky.anchorY = 0

Anchor points range from 0 … 1 where 0 is either the left or top edge, 1 is the right or bottom edge. 0.5 is the center. 

Rob

I see you got an answer to this on the community slack, but so the answer will be searchable and usable by others.

Many years ago (pre-2013) there were some inconsistencies in our API’s. for instance display.newImage() would center the object as would display.newCircle() and other API’s like display.newRect() would position using the top, left corner. When we released Graphics 2.0 in 2013, we made all of our API’s consistent, and now they all default to the center of the object for positioning.

You can either use the center of the object or if you want to use the top, left corner, you can use anchor points:

local sky = display.newRect( screenLeft, screenTop, screenWidth, screenHeight) sky.anchorX = 0 sky.anchorY = 0

Anchor points range from 0 … 1 where 0 is either the left or top edge, 1 is the right or bottom edge. 0.5 is the center. 

Rob