Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

Simple question! on placing Rect in the center of the screen
Started by ipopon Oct 11 2018 02:23 AM

1 reply to this topic
screen coordinates screen coordinates centerx display.contentcenterx screenleft screenleft * 2
This topic has been archived. This means that you cannot reply to this topic.
[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Observer

  • 1 posts
  • Corona SDK

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! 

[TOPIC: post.html]

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,687 posts
  • Enterprise

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.