Corona SDK - rectangle not appearing in expected place (coordinate system) - coronasdk

I'm brand new to corona SDK and following tutorials. I notice that when I'm positioning elements theyre not appearing as they do in the tutorials. For example:
local testRect = display.newRect( 0, 0, 50, 50 )
testRect:setFillColor( 0.5,0,0 )
In the tutorial it looks like this:
I would guess the anchor point on the rectangle would be 0.5, 0.5 and I'd have to position it 50% to the right, and 50% from the top of the upper left corner. It looks like in their example the rectangle has an anchor point of 0, 0 and is stuck to the edge of the screen by default.
Why is there a difference? i notice this tutorial is using an iphone 4.. but should that matter?

A couple of years ago, Corona SDK went to their Graphics 2.0 standard. Part of this move was to get all display.* objects to behave the same when creating them. Before Graphics 2.0, some objects the X and Y values meant top left, for others it meant center. With Graphics 2.0, all X and Y values when creating an object are the center of the object now.
If you have an older tutorial, it may be assuming that the x, y is top left. In your example you are creating the object at 0, 0 which is the top left corner of the content area. Since you're on a screen that's wider than it's defined content area, 0, 0 isn't always the top left corner of the screen.
I'm guessing your config.lua is setting the width to 320 and the height to 480 (always listed as if the device was in portrait mode). But you're using an iPhone 5 screen, which means the top left corner is -44, 0. This explains why the image isn't all the way to the left edge.

I am sure the anchor points they using are 0.5,0.5.
I suggest you check your build.settings file .. I tried the code and got the same result they had ..
Again CHECK your build.settings file

Related

Offset two child-charts within a VictoryChart

I have a boxplot and a scatterplot within a VictoryChart and want them shifted a number of pixels in the non-domain axis.
My current code and resulting chart are here: https://codesandbox.io/s/beautiful-violet-opclp?file=/index.js
Now I want the red triangle to be placed a bit higher, so about 15 pixels in the y-direction upwards. I tried padding, domainPadding, dx, dy, but nothing works really.
Any hints are appreciated!
By creating a custom point, the actual position can be set. See https://codesandbox.io/s/focused-lederberg-i8ddx?file=/index.js

Why do anchor points vary in different resoliutions?

I'm currently trying to place text in the four corners of the screen but the thing I came across was that in some screen resolutions(e.g. 1080*1920) the anchor points aren't right in the corner. the x values for some reason are the same, but the y changes, and is not close to the corner of the screen. Here is an example of me placing some text in the top right corner:
local myText = display.newText( "RIGHT", 0, 0, native.systemFont, 16 )
myText:setFillColor( 0, 0, 0 )
myText.anchorX = 1
myText.anchorY = 0
myText.x = display.contentWidth
myText.y = 0
I can't understand why this doesn't work for all screen resolutions.
Will this work for you:
-- Top
myText.y = display.screenOriginY;
-- Bottom
myText.y = display.contentHeight - display.screenOriginY;
-- Right
myText.x = display.contentWidth - display.screenOriginX;
-- Left
myText.x = display.screenOriginX;
Tha anchor points of display objects don't change.
The coordinate system of screen change depends on scaling mode. So top-left point not always will be (0, 0). For example in letterbox mode top-left point would be (display.screenOriginX, display.screenOriginY).
From Corona documentation
"letterbox" — scales the content area to fill the screen while preserving the same aspect ratio. The entire content area will reside on the screen, but this might result in "black bars" on devices with aspect ratios that differ from your content aspect ratio. Note, however, that you can still utilize this "blank" area and fill it with visual elements by positioning them or extending them outside the content area bounds. Essentially, "letterbox" is an ideal scale mode if you want to ensure that everything in your content area appears within the screen bounds on all devices.
"zoomEven" — scales the content area to fill the screen while preserving the same aspect ratio. Some content may "bleed" off the screen edges on devices with aspect ratios that differ from your content aspect ratio. Basically, "zoomEven" is a good option to ensure that the entire screen is filled by the content area on all devices (and content clipping near the outer edges is acceptable).
Letterbox
zoomEven
Read more about Content Scaling.

How do I align circular buttons in a circle in both landscape and portrait orientations on iOS?

I would like to layout 5 buttons (that have a circular image) in a circular pattern (a kind of pentagon shape) in iOS. Ultimately it will also work on iPad, but at the moment, I'm just trying to get it working on iPhone.
So far, I have just tried putting them into a UIView and using constraints to lay them out via IB. However, when I rotate the device to landscape the layout gets messed up.
Here is a picture of the kind of layout I mean in IB which should ideally adapt to fit both iPhone portrait and landscape.
If I fix the height and width, it's clearly too big for iPhone:
I guess I need to find a way to have it maintain it's aspect ratio as a square and aligned to the middle of the parent view.
If I constrain to the top layout guide with horizontal space and vertical space it just looks like this:
I just can't seem to get my head around the right way to do this.
Any thoughts and pointers in the right direction greatly appreciated.
Many thanks.
You need to update your approach,
Logic : Use parametric equations to get points ( button centres ) along circumference of circle, Update constraints when screen rotates ( take outlets of constraints to modify ).
These formulae will give point ( x, y ) along circumference ( Centres of buttons / leading & top constraint of buttons )
x = cx + r * cos(a)
y = cy + r * sin(a)
Where,
r is the radius, ( in your case half of screen size )
cx,cy the origin, ( in your case centre of view )
and a the angle from 0..2PI radians or 0..360 degrees. ( Angle between two buttons 360/5 because there are 5 buttons )
I have used this formula in one of the control i have developed..
https://github.com/AdityaDeshmane/iOSCircularMenu
Check method - (void)setButtonFrames in following file
https://github.com/AdityaDeshmane/iOSCircularMenu/blob/master/iOSCircularMenu/CustomCircularMenuControlFiles/ADCircularMenuViewController.m

Position with Lua in Corona SDK confusion

I'm using corona simulator. I displayed display.contentWidth and display.contentHeight .The width is 320 and height is 480. I've also made a text saying "Hello", and played with positioning.
I've noticed the x values looked pretty accurate, I would set x value of text to 0 and the center of it would be half covered and half revealed. Same results for x value of 320, but on the other side of the phone simulator.
When I set y value to 0 it sits right under the Carrier and fake signal strength symbol, fully revealed. When I set the y value to 480 the heights max, it would have about an inch gap from the bottom of the phone. -- My thoughts are that it should be half covered vertically both ways :-S
In corona simulator i'm viewing it in Iphone5 , and notice when I change to Iphone it would reveal as I would expect. With Iphone 5, droid, and some other view it would give me these results.
* My question is would my program look the same on all devices, or is corona simulator accurate displaying what it would look like. If it displays different on variety of devices, Why?
width = display.newText (display.contentWidth .. "px is Width", 200, 200, nil, 30)
height = display.newText (display.contentHeight .. "px is Height", 150,300, nil, 30)
text = display.newText ("Hello", 0, 480, nil, 30)
The resolution of the iPhone 5 is different to the iPhone 4 (obviously), but rather than receiving different display.content* values, Corona handles it using something called display.screenOrigin. This is the difference between the display being used and the standard display (iPhone/iPhone 4). If you print these values, for example:
print("Origin x = "..tostring(display.screenOriginX))
print("Origin y = "..tostring(display.screenOriginY))
Then you will see that one of them, depending on your simulator's orientation, will be "-44". This is the difference in pixels. In your example for what I assume is the portrait orientation, you would need to position Y for top and bottom like so:
--For positioning at 'true 0'
obj.y = 0 + (display.screenOriginY)
--Or, for positioning at "true bottom"
obj.y = 0 - (display.screenOriginY)
Hope this helps. For more on display.screenOrigin, see the Corona documentation for X and the Corona documentation for Y
Cush
Every object has a rectangular boundary as in figure 1 below. Here there is a text object with boundary indicated with black colour.
The red dot at the top is that objects default anchor point.
If you are not specifying the text's x and y positions as (obj.x=... & obj.y=...), then corona will position the object according to the points in the display object creation line (i.e.., display.newText(""......)). For example, if you have a code like:
local text = display.newText ("myText", 0, 0, nil, 30)
then the text will be placed such that the red dot is at (0,0). It is why you saw it under the status bar.
General object placement in corona is as below:
Underlined myText indicates the different positioning of the text object according to points in object creation line. i.e.;
local text = display.newText("myText",x,y,nil,30)
_w ==> display.contentWidth
_h ==> display.contentHeight
Keep Coding.................. :)

Corona SDK: fill up a bar from left to right

I'm learning Corona SDK and am new to lua as well (i mainly do ruby and some javascript).
I have a bar that i want to fill up as the user does stuff. I've set it up as follows:
--outer rectangle
powerBar = display.newRect(210, 6, 24, 9)
powerBar.strokeWidth = 1
powerBar:setStrokeColor(254,203,50)
powerBar:setFillColor(0,0,0,0)
--inner rectangle which fills up
powerBarFill = display.newRect(211,7,0,7)
powerBarFill:setFillColor(234,183,30)
When the "stuff" happens, i add 1 to powerBarFill.width, which i thought would make it grow from left to right. But, it's actually growing out from the centre, ie its x is in the centre and the width extends either side from that.
Whats the best way to keep the left side static and grow the right side? Can i set it so that it's x position is actually on the left hand side rather than in the middle? Seems like that might do it.
cheers in advance
I've run into this problem as well when creating a progress bar. The problem is with the rect's reference point. The default reference point is in the center of an object, as you've noticed. You can use object:setReferencePoint() to change it. I believe you want to use the display.BottomLeftReferencePoint value:
powerBar:setReferencePoint(display.BottomLeftReferencePoint)
Keep in mind that you have to set this value before you set your x,y values. So in your case you'll need to set the reference point after creating the rectangle, and then assign values to x,y again (even though you already did this in the newRect constructor):
powerBar = display.newRect(210, 6, 24, 9)
powerBar:setReferencePoint(display.BottomLeftReferencePoint)
powerBar.x, powerBar.y = 210, 6
If it's width is from the X position on both sides:
1) It should start at:
Centre - (Width when it's full / 2)
2) Every frame, add:
incrs = 1 --Amount to increase by
width = width + incrs
x = x + incrs / 2

Resources