Centering a level?

Hi there,

I’m pretty new to coding, so forgive me if there is a pretty obvious answer to my question!

When you create a level that isn’t bigger than the device resolution, how would you go about centering it to the middle of the screen? I’ve tried using map:move and map:setPosition, but neither helped.

http://i40.tinypic.com/x1ksk1.jpg

I hope that makes sense! [import]uid: 12217 topic_id: 19152 reply_id: 319152[/import]

Something like this should work:
[lua]local urLevel = display.newImage(…) --put ur image on the screen
urLevel:setReferencePoint(display.CenterReferencePoint ) --let corona know you want to use the center as the reference point
urLevel.x = (display.contentWidth * .5) – set the x value to 1/2 the width
urLevel.y = (display.contentHeight * .5) – set the y value to 1/2 the height[/lua]
[import]uid: 19193 topic_id: 19152 reply_id: 73893[/import]

Hi Scott,

My level is a .tmx file (not an image) which I’m loading with Lime, so what you’ve proposed won’t work in this case. :frowning:

Thanks for the suggestion though :slight_smile: [import]uid: 12217 topic_id: 19152 reply_id: 73901[/import]

This should do it ( at least it does for my test map ):

map:disableScreenClamping()  
map:setPosition( display.contentCenterX - map:getVisual().contentWidth, display.contentCenterY - map:getVisual().contentHeight / 2 )  

http://dl.dropbox.com/u/571145/smallMap.png [import]uid: 5833 topic_id: 19152 reply_id: 73948[/import]

Thanks for the direction Graham! A straight copy and paste of your code got me half the way there (it was centred vertically, but not horizontally), so I modified it to this and got it working:

map:disableScreenClamping() map:setPosition(map:getVisual().contentWidth / 2, map:getVisual().contentHeight / 2)

Thanks again! [import]uid: 12217 topic_id: 19152 reply_id: 74683[/import]

Whoops, my bad. Well I was close :slight_smile: Glad you got it all working! [import]uid: 5833 topic_id: 19152 reply_id: 74718[/import]

I think I’ve found a bug.

So I’ve got my level perfectly centered using the above code, as can be seen here: http://i41.tinypic.com/nmdyc0.jpg

But as soon as I add an object layer to my map and insert an object (the player start) it throws my level completely off center: http://i40.tinypic.com/2vtuv81.jpg

Any thoughts? I hope this information helps! :slight_smile: [import]uid: 12217 topic_id: 19152 reply_id: 74816[/import]

It could be that the objectLayer is changing the contentWidth value of the map.

If possible could you make a simple demo with the issue reproducible and then send it to support@justaddli.me and I will take a look into it. [import]uid: 5833 topic_id: 19152 reply_id: 74912[/import]