Please help me understand the display.newRect()

Problem:

     The newRect( ) is not behaving as I expect it to according to the online api docs. 

This is my code:

     local block = display.newRect( display.contentCenterX, display.contentCenterY, 100, 100)

What I saw:

     

    Picture attached. 

What I expected to see:

     I expected to see the center of the cube placed in the center of my screen. But as you can see, the top left corner of the cube was placed in the center of my screen instead.

Environment:

    Build: 2906

    Width: 320

    Height: 480

    Scale: letterbox

    xAlign = center

    y Align = center

    simulation viewed as: iPhone 6

Hi and welcome to the forums. Thank you for a really well asked question (though your attachment didn’t attach!).

You should be seeing a centered rectangle with that code. However there are a couple of things that could change that. If you have code like:

block.anchorX = 0 block.anchorY = 0

Then that tells Corona SDK to change X, Y from the center to the top left corner. There are also ways to make that the default too.

If you have commands like:

display.setDefault( "anchorX", 0) display.setDefault( "anchorY", 0)

That will cause all objects to orient top left.

Last minor point, you reference build 2016.6.21. That’s the day the build was built. There is a 4 (or 8) digit number that we generally use when referencing builds. It’s something like 2016.2906. We generally use the last four digits (2906). For some reason we remember what goes with that number instead of the date it came out.

Thanks Rob for the help.

I’ve corrected the build number and attached my screenshot.

I understand --> display.setDefault( “anchorX”, 0), and I have not used that. I only have this one line of code:

 

local block = display.newRect( display.contentCenterX, display.contentCenterY, 100, 100)

I still don’t get why the upper left corner of the cube is placed in the center of the screen instead of the the cube’s middle point.

I tried the code sample and it worked for me.  It placed the rect in middle of the screen (CoronaSDK-2917).

 local block = display.newRect( display.contentCenterX, display.contentCenterY, 100, 100)

I figured out why my square was not centered

In my config.lua file, I had this line:

graphicsCompatibility = 1

When I commented out the above line, the cube was centered.

That would do it!

Hi and welcome to the forums. Thank you for a really well asked question (though your attachment didn’t attach!).

You should be seeing a centered rectangle with that code. However there are a couple of things that could change that. If you have code like:

block.anchorX = 0 block.anchorY = 0

Then that tells Corona SDK to change X, Y from the center to the top left corner. There are also ways to make that the default too.

If you have commands like:

display.setDefault( "anchorX", 0) display.setDefault( "anchorY", 0)

That will cause all objects to orient top left.

Last minor point, you reference build 2016.6.21. That’s the day the build was built. There is a 4 (or 8) digit number that we generally use when referencing builds. It’s something like 2016.2906. We generally use the last four digits (2906). For some reason we remember what goes with that number instead of the date it came out.

Thanks Rob for the help.

I’ve corrected the build number and attached my screenshot.

I understand --> display.setDefault( “anchorX”, 0), and I have not used that. I only have this one line of code:

 

local block = display.newRect( display.contentCenterX, display.contentCenterY, 100, 100)

I still don’t get why the upper left corner of the cube is placed in the center of the screen instead of the the cube’s middle point.

I tried the code sample and it worked for me.  It placed the rect in middle of the screen (CoronaSDK-2917).

 local block = display.newRect( display.contentCenterX, display.contentCenterY, 100, 100)

I figured out why my square was not centered

In my config.lua file, I had this line:

graphicsCompatibility = 1

When I commented out the above line, the cube was centered.

That would do it!