Corona SDK distribution and size of objects - lua

How to indicate the sizes of objects and their coordinates when developing a mobile game. For buttons, I tried to indicate a percentage of the screen size (local scrPercentW = display.actualContentWidth / 100) or division of a constant display.contentCenterX (display.contentCenterY). But in the simulator on some devices it’s right, but at some intervals and sizes it’s wrong.
For example, I have four buttons on the menu that I need to arrange at the same distance from each other and from the edges of the screen. How should I do it?

I see that you resolved the problem of display.contentWidth, while for the size of a text you just need this:
local someText = display.newText( "here goes your text", 20, 80 )
someText.size = 30

Related

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.

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

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

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

Repeating 2d world

How to make a 2d world with fixed size, which would repeat itself when reached any side of the map?
When you reach a side of a map you see the opposite side of the map which merged togeather with this one. The idea is that if you didn't have a minimap you would not even notice the transition of map repeating itself.
I have a few ideas how to make it:
1) Keeping total of 3x3 world like these all the time which are exactly the same and updated the same way, just the players exists in only one of them.
2) Another way would be to seperate the map into smaller peaces and add them to required place when asked.
Either way it can be complicated to complete it. I remember that more thatn 10 years ago i played some game like that with soldiers following each other in a repeating wold shooting other AI soldiers.
Mostly waned to hear your thoughts about the idea and how it could be achieved. I'm coding in XNA(C#).
Another alternative is to generate noise using libnoise libraries. The beauty of this is that you can generate noise over a theoretical infinite amount of space.
Take a look at the following:
http://libnoise.sourceforge.net/tutorials/tutorial3.html#tile
There is also an XNA port of the above at: http://bigblackblock.com/tools/libnoisexna
If you end up using the XNA port, you can do something like this:
Perlin perlin = new Perlin();
perlin.Frequency = 0.5f; //height
perlin.Lacunarity = 2f; //frequency increase between octaves
perlin.OctaveCount = 5; //Number of passes
perlin.Persistence = 0.45f; //
perlin.Quality = QualityMode.High;
perlin.Seed = 8;
//Create our 2d map
Noise2D _map = new Noise2D(CHUNKSIZE_WIDTH, CHUNKSIZE_HEIGHT, perlin);
//Get a section
_map.GeneratePlanar(left, right, top, down);
GeneratePlanar is the function to call to get the sections in each direction that will connect seamlessly with the rest of your world.
If the game is tile based I think what you should do is:
Keep only one array for the game area.
Determine the visible area using modulo arithmetics over the size of the game area mod w and h where these are the width and height of the table.
E.g. if the table is 80x100 (0,0) top left coordinates with a width of 80 and height of 100 and the rect of the viewport is at (70,90) with a width of 40 and height of 20 you index with [70-79][0-29] for the x coordinate and [90-99][0-9] for the y. This can be achieved by calculating the index with the following formula:
idx = (n+i)%80 (or%100) where n is the top coordinate(x or y) for the rect and i is in the range for the width/height of the viewport.
This assumes that one step of movement moves the camera with non fractional coordinates.
So this is your second alternative in a little bit more detailed way. If you only want to repeat the terrain, you should separate the contents of the tile. In this case the contents will most likely be generated on the fly since you don't store them.
Hope this helped.

Resources