display.contentWidth not working

Hi there,

I have elements on my screen which i set using
[lua]object.x=display.contentWidth*0.5[/lua]

This works fine on iPad/iPhone and on the simulator under both Android and iPhone.

But, when I build the application and load it onto my samsung galaxy s II, the objects are no longer in the center of the screen.

What can I do?

Thanks [import]uid: 24641 topic_id: 29016 reply_id: 329016[/import]

do you have a screen size specified in your config.lua file? I’m not sure if that works without a screen size specified… [import]uid: 164950 topic_id: 29016 reply_id: 116778[/import]

I have width and height set at 320 and 480 respectively [import]uid: 24641 topic_id: 29016 reply_id: 116786[/import]

Here is my best guess: When you create a display object for the first time such as text, usually what I do is use default (0, 0) x,y coords and then after I set the reference point do I actually position it to fit my layout. So my guess is that they spawn at some default value and then there might be an error in your code which keeps it from reaching the point where you position them? As an error in code I mean that devices are case sensitive but the simulator is not. I had the issue so it’s worth checking. [import]uid: 164950 topic_id: 29016 reply_id: 116789[/import]

Hi there,

One idea is, instead of [lua]display.contentWidth*0.5[/lua], to try [lua](display.contentWidth - display.screenOriginX*2)*0.5[/lua]. If you’re using letterbox scaling (specified in your config.lua file), the width of the entire screen in virtual coordinates is not just [lua]display.contentWidth[/lua], but also two [lua]display.screenOrigin[/lua]'s (for the left and right). Since [lua]display.screenOrigin[/lua] is negative, I subtract it rather than add it.

  • Andrew [import]uid: 109711 topic_id: 29016 reply_id: 116790[/import]

Have you tried this?

[code]
object.x = display.contentWidth/2
[import]uid: 41496 topic_id: 29016 reply_id: 116804[/import]

@intoitgames I have looked into it but code executes after the placement of the objects - so it can’t be that!

@aukStudios thanks for the advice but that produced the same results
I’m kind of stuck here! [import]uid: 24641 topic_id: 29016 reply_id: 116820[/import]

mdcapps,

Roughly how far off-center would you say the object is on the device? Does the amount by which it’s off-center seem to vary if you try it with a larger or smaller object? I just want to get a sense of how big the deviation is.

Also, which scaling mode are you using, if any (specified in you config.lua file)?

Last, is your app oriented in portrait or in landscape?

  • Andrew [import]uid: 109711 topic_id: 29016 reply_id: 116822[/import]

Hi, here is a photo of the problem:
https://www.dropbox.com/s/pj3jdyedp69z7yx/SC20120725-130501.jpeg
As you can see, the text in the rectangles is to the left, when it should be at the centre!

In config.lua i’m using ZoomStretch

Thanks
[import]uid: 24641 topic_id: 29016 reply_id: 116825[/import]

Hi mdcapps,

Hmm, that’s definitely very strange, especially that (a) it’s device dependent, and (b) instead of being centered, the text is perfectly left-aligned. I’d be curious if it happens not just on the Samsung Galaxy S II, but also on other Android devices (if you have access to one)? I’d also be curious to see a screenshot from the iPhone or iPad, where you said it is working properly, for comparison.

  • Andrew [import]uid: 109711 topic_id: 29016 reply_id: 116934[/import]

Here is a photo of it being centre-aligned on the simulator under Android:
http://cl.ly/image/3f3r042w1r2C

and here is a photo of it working fine on the iPhone

https://www.dropbox.com/s/5map9n3ym0lr5lu/2012-07-26%2013.44.12.png

I don’t have access to any other android devices unfortunately!

This is very odd!

Max [import]uid: 24641 topic_id: 29016 reply_id: 116987[/import]

display.contentWidth * 0.5 and display.contentWidth / 2 result in the exact same value. The former is microscopically faster.

Corona SDK also offers:

display.contentCenterX and display.contentCenterY which does the multiply by 0.5 for you, but it’s done once at the begging of the app load so you don’t have to keep doing the math later, which will improve performance slightly better than the two methods above.

[import]uid: 19626 topic_id: 29016 reply_id: 116995[/import]

Wow, that it is indeed extremely bizarre. Seems to me that it must be a bug. I’m guessing it affects all Android devices, not just the Samsung Galaxy S II. Fortunately, that’s relatively easy to test, since if you have any friend with an Android phone, you can just email them your .apk file and they can install it and tell you what they see.

Either way, this does seem to me like a bug, so I would think the best next step is to file a bug report.

  • Andrew [import]uid: 109711 topic_id: 29016 reply_id: 117108[/import]