Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

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
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

ipopon

[GLOBAL: userInfoPane.html]
ipopon
  • 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]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,853 posts
  • Corona Staff

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




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: screen coordinates, screen, coordinates, centerx, display.contentcenterx, screenleft, screenleft * 2