Automatically change the position of objects when using different devices - lua

I am still new to lua and corona (3 days to be exact) and I'm trying to my objects on the screen using multiple devices. From what I understand, to make the objects automatically reposition themselves, I need to get the screen size and do some calculation to it to make the necessary adjustment. Here's a sample of what I'm trying to do:
obj1 = display.contentWidth*0.50
This will then put my object to the middle of the screen but there are instances wherein this doesn't seem effective so my question is, what is a better approach to automatically reposition my objects? Especially objects that are no longer in the center of the screen.

I'm doing it in a different way. I think this may help you. As my opinion, just create multiplier values for width and height(according to which simulator you are coding in), and multiply your each width or height parameter with this(as below):
--------------------------------------------------------------------------
-- choosing xMultiplier and yMultiplier values --
--------------------------------------------------------------------------
local xMultiplier = display.contentWidth/320
local yMultiplier = display.contentHeight/480
--[[ I used 320 here because i'm using iPhone Simulator
(320 is the width of the simulator you are coding in)
I used 480 here because i'm using iPhone Simulator
(480 is the height of the simulator you are coding in)--]]
--------------------------------------------------------------------------
-- creating background and positioning it --
--------------------------------------------------------------------------
local bg = display.newImageRect("bg.png",320*xMultiplier,480*yMultiplier)
bg.x = 160*xMultiplier ; bg.y = 240*yMultiplier
--------------------------------------------------------------------------
-- creating object and positioning it --
--------------------------------------------------------------------------
local rect = display.newImageRect(0,0,50*xMultiplier,50*yMultiplier)
rect.x = 160*xMultiplier ; rect.y = 100*yMultiplier
--------------------------------------------------------------------------
Note: If you are using config.lua file in your project, this may not work.
Pros: This needs only one image.
Cons: It may affect the clarity of images in devices with high resolution. So choose an image with suitable resolution.
keep coding... :)

The way I do this is based off of the principle that things are going to positioned based on a side of the device or from the center. I'm going to use a horizontal app for this, btw.
Somethings need to be anchored to the left or right edges. Somethings need anchored to the top of the screen and other things need to be some distance away from center.
Consider this image:
The Home Icon and settings icon need to be a few pixels away from the left edge. They also need to be a few pixels down from the top. I would position these using a fixed number:
homeIcon.x = 32
homeIcon.y = 32
gearIcon.x = 32
gearIcon.y = 64 -- or whatever numbers they really are.
The score on the right needs to be a fixed distance away from the right edge and top:
scoreText.x = display.contentWidth - 64
scoreText.y = 32
The Turkey on the bottom's position shouldn't change based on the shape of the device's width, but could move based on the height of the device.
turkey.x = display.contentCenterX
turkey.y = display.contentHeight - 64
On an iPad which is less wide than an iPhone in this example, the home, gear and score buttons will end up closer to center. On an iPhone 5 or a wide screen Android, they spread out.
The example above doesn't really help on some of the centering ideas. I don't have a screen shot for this, but lets say you were laying out a login form for a game where you are collecting the Username and password and you had a submit button. You could position them like:
usernameField.y = display.contentCenterY - 64
passwordField.y = display.contentCenterY
submitButton.y = display.contentCenterY + 64
Then regardless of the height of the device, those UI elements would stay the same distance from the center making your form look good. If you used a fixed distance from the top, the form would move based on the size of the device. Here their positions stay the same.

Related

React Native same size circle on different displays

I'm new to React Native and I'm very happy with how flexbox automatically fits the content to the screen size.
This works great as long as I'm working with squares, but when it comes to circles it seems that I have to specify the size of the circle using pixels..
This is obviously not optimal when developing to different screen sizes.
I have tried using PixelRatio like this:
height: 90 * pixelRatio,
width: 90 * pixelRatio,
borderRadius: 45 * pixelRatio,
This makes it a little better, but there still seems to be a huge different in the size of the circle as it appears on Iphone 6 vs Iphone 6 plus.
Is there any way to use flexbox to automatically create the size of a circle just like it can with squares?
Or is there another way to make sure the circle will be displayed identical on different devices?
AFAIK, the value you enter for width and height are device independent pixels. Meaning they will scale according to the pixel density of the device in hand. pixelRatio will make it extremely device dependent.
Have to tried it without the pixelRatio factor
height:90,
width:90,
borderRadius:45

Corona sdk best way to go about build.settings and config.lua

I am trying to make a math game but I have come into several problems. The first is that In the android emulator that comes with corona I have black space at the top and bottom. These are my files:
config.lua:
local aspectRatio = display.pixelHeight / display.pixelWidth
application = {
content = {
width = 320
height = 480
scale = "letterBox",
fps = 30,
},
}
build.settings:
local aspectRatio = display.pixelHeight / display.pixelWidth
application = {
content = {
width = 320
height = 480
scale = "letterBox",
fps = 30,
},
}
It shows my image correctly but I get black at the top and bottom. I read tutorials about these to files, but they are so complex. What is the easiest way to may these files work? The tutorials I read also tried to find each device that this app could be running on and makes the settings accordingly, but id like to be more cross-platform and not have to update the app every time their is a new size of device.
In regards to the black bars at the top and bottom, that is a side-effect of "letterbox" scaling.
Corona automatically scales your content so that it fills as much of the screen space as possible, WHILE maintaining aspect ratio of your original content. This means that it will scale the content up until it hits either the top/bottom edge or the left/right edge of the screen.
When it hits an edge, it stops scaling the content. That may result in the black bars you're seeing right now.
This tutorial here is the most comprehensive tutorial available.
That tutorial has a file (the ultimate config.lua file) that you can download and use in your app's build. Also found here!

different screen sizes with corona sdk

I am making my first iphone game using corona sdk and would like it to run on as many devices as possible (phones + tablets). However I am not sure how to deal with different screen sizes and resolutions. I developed my game for the iPhone 5 using corona simulator and it works fine on that device. When I tried it on lower resolution devices like the iPhone 4 I get 2 black rectangles on each side. I tried creating 2 different backgrounds with different resolutions and added this in my config.lua:
imageSuffix = {
["#2x"] = 2
}
However this does not seem to change anything... I am not sure what height and width I should set in content in the config.lua file and what heights and widths I should set for the backgrounds. I am sorry if these questions are stupid, I am just starting.
Thanks in advance!
It sounds like you need to fully read up on config files and dynamic scaling.
The question is a little to broad as such I suggest you read this article about "the ultimate config/modernizing the config".
Some screens are wider while others are more narrow. If we take
resolution out of the equation, its easier to visualize the screens.
Corona makes it easy to take resolution out of the picture using
Dynamic Scaling. With Dynamic Scaling, you can use a common set of
screen coordinates and Corona will automatically scale the text and
graphics for different resolution screens. It can scale upwards or
downwards depending on your starting point. It also can substitute
higher resolution images when it needs to scale up. This is all
managed by a Lua file in your project folder called config.lua.
Since available resolutions vary considerably, it’s helpful to use the
same scale for each device. It doesn’t matter if you’re on an iPhone
3GS at 320×480 or a Retina iPad at 1536×2048, the location (0,0)
represents the top-left corner and (320,480), in vertical portrait
mode, is the bottom-right corner. The screen center is (160,240).
Each point, in this case, is one pixel on a lower-resolution device
like the 3GS, which has a native screen resolution of 320×480, while
each point is four pixels on a Retina iPad. Don’t worry about the math
— Corona will handle it for you.
Source: http://coronalabs.com/blog/2012/12/04/the-ultimate-config-lua-file/
This goes through the creation of a config file that fully utilize the dynamic scaling and image scaling.
local aspectRatio = display.pixelHeight / display.pixelWidth
application = {
content = {
width = aspectRatio > 1.5 and 320 or math.ceil( 480 / aspectRatio ),
height = aspectRatio < 1.5 and 480 or math.ceil( 320 * aspectRatio ),
scale = "letterBox",
fps = 30,
imageSuffix = {
["#2"] = 1.8,
["#4"] = 3.6,
},
},
}

How to make my Gideros game adaptable to all screen sizes?

I am quite new to Gideros and game development as well.
I am trying to build a game, it looked fine with the Gideros player, but when I tried with an Android phone, the background was way too small. I changed its properties to autoscaling so as to fit in with the width. The background now fits in the width, but other objects seem to go to absurd locations.
Though I was using W = application:getDeviceWidth(), H = application:getDeviceHeight(), and while setting up the location, used W/2 and H/2 instead of hardcoding it.
However, this object that seems to shift to rightmost bottom end(in landscape left mode) was right at its center if I do not apply fit width property.
What can I do to fix it?
You should use W = application:getContentWidth() , H = application:getContentHeight() which would return logical dimensions that are used when in scaling mode.
Basically it all brings down to these points:
1) Choose the scaling mode that is proper for your game (Letterbox being most popular)
2) Choose logical dimensions for your game and create all the graphics for logical dimensions you set in the project properties (recommended 480x800 or 640x960)
3) Create backgrounds a little more bigger than logical dimensions to cover whitespaces on devices with different ratios
4) Use absolute positioning (http://appcodingeasy.com/Gideros-Mobile/Ignore-Automatic-Screen-Scaling-when-positioning-objects) for objects that need to stick to sides of the screen as on screen buttons for example
5) (Optionally) prepare bigger graphics in in some fixed ratio coefficient and use Automatic Image Resoltuion feature to automatically load them for bigger devices
More information available here:
http://members.giderosmobile.com/knowledgebase.php?action=displayarticle&id=79
Addition: (Difference between device and logical dimensions)
Device dimensions is exactly what device has. Meaning on an iPhone 3GS it will return width as 320
But logical dimensions are exactly what you set in your project properties. No matter what resolution you have, the logical dimensions will always be the same. They basically will be scaled based on the scale mode you choose.
Here are more specifics on that topic: http://appcodingeasy.com/Gideros-Mobile/Difference-between-content-logical-and-device-dimensions-in-Gideros-Mobile
So if you are developing only for one specific resolution, you can use Device dimensions, otherwise it is suggested to use Logical dimensions with the scale mode you find suitable.

Adaptive Positioning Based on iOS Device

I made an iPhone game a few months ago, and am now trying to port it as a universal app to both the iPad and iPhone 5 with Cocos2D. I was wondering if there was a simple-ish way to determine where an object should be placed based on the device running the game.
I could use if statements to figure out which device the game is running on, so when I get the correct sized images for the device I could have separate positions for each object, but it seems like there would be a maths formula which would allow me to use a lot less code. Obviously something like a full screen background is very simple, because it just needs to be centred with:
[background setPosition:CGPointMake(screenSize.width/2,screenSize.height/2)];
I haven't a clue how to adapt a button that would be X = 144 & Y = 330 on the old 3.5inch, 640 by 960 resolution iPhone to an iPad or iPhone 5 resolution.
I'm willing to use a more recent version of iOS if it will make my life easier, but because I'm not using any of Apple's objects I don't know if that is possible.
Maybe this isn't even possible because the button will be different sizes for the iPhone and iPad version, but I thought I would ask.
yeah, i am usually facing the same problem,
but if it is just a static objects placement
i would have relative coordinates instead of absolute for every object
and then use screen sizes to place them correctly
so you might want to use a function like:
-(CGPoint) relativeToScreen:(CGPoint) p {
return ccp(screenSize.width * p.x, screenSize.height * p.y)
}
where 0.0 <= p.x =< 1.0 and the same for p.y
and don't forget about your anchorPoint, because the node position is based on it as well
and i hope you have discovered that cocos2d already does image choosing instead of you,
you just have to set right suffixes for your images: -hd, -ipad, -ipadhd
For iphone5 resolution, I position hud buttons relative to the screen dimensions. Very similar to what you are doing for the background. So for example, a pause button I want in the top left I would position like this:
[pauseButton setPosition:CGPointMake(0.0f + 30.0f, screenSize.height - 50.0f)];
For ipad it gets really tricky. The lazy way which I have implemented is to play around with the content scale factor and zoom everything up and have "dead" borders to compensate for the ipad's screen ratio. Not the best, but at least you can re-use all the same assets for the ipad.

Resources