Problem with Display Object (PLEASE HELP)

Hello

Anyone can help me with displaying a rectangle please. Because it is not working according to CoronaSDK explanation.

My purpose is to display a small rectangle and I want it to stay on the most left top corner while the rectangle is visible 100%.

To achieve my goal I was forced to put following coordinates and it did work. But it is not according to CoronaSDK explanation.

local myBox = display.newRect( 10, -34, 20, 20 )
myBox:setFillColor( 1, 0, 0, 0.8 )
myGroup:insert( myBox )

What the problem might be? please help.

Thanks a lot in advance.

Ragamer (Rahim)

So your trying to display a 20x20 rect in the top left corner of every device?

–SonicX278

Download this and take a look at how i calculated the left, right, top, and bottom, then put rectangles there:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/10/lgg4.zip

This was to answer another question last year, but it has what you need to solve your question.

This file has the calculations:

http://github.com/roaminggamer/RG_FreeStuff/blob/master/AskEd/2015/10/lgg4/common.lua

Yes. the size of my rectangle is 20x20.

So According to what I learned, if I make X and Y at zero (0) the then on the top-left I should see 1/4 of the rectangle. Therefore to make it appear fully I should code as bellow, if I learned correctly.

local myBox = display.newRect( 10, 10, 20, 20 )

Ideally with these parameters Rectangle should appear on the most left at X and most top at Y. But in actual at X it appears on the most left (which is what I want), but on Y it is appears below the top.

So to adjust it to the most top-left I made Y=-34 as below.

local myBox = display.newRect( 10, -34, 20, 20 )

So, X=10, Y=10 is correct but gives incorrect result

But, X=10, Y=-34 is incorrect but gives Correct result

The rect is appearing below the top of the screen because not all devices/screens use the same aspect ratio and so they have extra space around the edges.  

If your config.lua file states that the game is 640*960 but you are viewing on a device which is “taller” than an iPhone 5 you will have “extra” space at the top and bottom of your screen. So 0, 0 will be slightly lower than the top left.  

Similarly if you then ran the same app on an iPad (which is comparatively wider than an iPhone 5) you would have space on the left/right and 0, 0 would be at the top of the screen but slightly away from the left hand edge.

You can do some fancy stuff with your config.lua file to make it fit, or use these values in place of 0 when positioning:

display.screenOriginX, display.screenOriginY

So instead of

local myBox = display.newRect( 10, -34, 20, 20 )

you would do

local myBox = display.newRect( display.screenOriginX + 10, display.screenOriginY - 34, 20, 20 )

Edit: and obviously you can store those variables to something that’s quicker to type:

--main.lua LEFT = display.screenOriginX + 10 TOP = display.screenOriginY --game.lua local myBox = display.newRect( LEFT + 10, TOP - 34, 20, 20 )

Thank you very much Alan. Your answer is very relevant to my question.

So your trying to display a 20x20 rect in the top left corner of every device?

–SonicX278

Download this and take a look at how i calculated the left, right, top, and bottom, then put rectangles there:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/10/lgg4.zip

This was to answer another question last year, but it has what you need to solve your question.

This file has the calculations:

http://github.com/roaminggamer/RG_FreeStuff/blob/master/AskEd/2015/10/lgg4/common.lua

Yes. the size of my rectangle is 20x20.

So According to what I learned, if I make X and Y at zero (0) the then on the top-left I should see 1/4 of the rectangle. Therefore to make it appear fully I should code as bellow, if I learned correctly.

local myBox = display.newRect( 10, 10, 20, 20 )

Ideally with these parameters Rectangle should appear on the most left at X and most top at Y. But in actual at X it appears on the most left (which is what I want), but on Y it is appears below the top.

So to adjust it to the most top-left I made Y=-34 as below.

local myBox = display.newRect( 10, -34, 20, 20 )

So, X=10, Y=10 is correct but gives incorrect result

But, X=10, Y=-34 is incorrect but gives Correct result

The rect is appearing below the top of the screen because not all devices/screens use the same aspect ratio and so they have extra space around the edges.  

If your config.lua file states that the game is 640*960 but you are viewing on a device which is “taller” than an iPhone 5 you will have “extra” space at the top and bottom of your screen. So 0, 0 will be slightly lower than the top left.  

Similarly if you then ran the same app on an iPad (which is comparatively wider than an iPhone 5) you would have space on the left/right and 0, 0 would be at the top of the screen but slightly away from the left hand edge.

You can do some fancy stuff with your config.lua file to make it fit, or use these values in place of 0 when positioning:

display.screenOriginX, display.screenOriginY

So instead of

local myBox = display.newRect( 10, -34, 20, 20 )

you would do

local myBox = display.newRect( display.screenOriginX + 10, display.screenOriginY - 34, 20, 20 )

Edit: and obviously you can store those variables to something that’s quicker to type:

--main.lua LEFT = display.screenOriginX + 10 TOP = display.screenOriginY --game.lua local myBox = display.newRect( LEFT + 10, TOP - 34, 20, 20 )

Thank you very much Alan. Your answer is very relevant to my question.