Tips and Tricks in Corona SDK

Hello,

Today I’m gonna share a few things which I have learned while working with Corona SDK. Those are things I wish somebody told me at the very beginning of my journey with Corona, so hopefully you find them useful as well 😉

Anchor Points

Anchor points are the properties which allow you to change the alignment of the object within its own bounding box. As difficult as it may sound, we’re just changing a gravity of some control (it can’t be a group, as groups don’t respect anchor points).

Whenever you see setReferencePoint, xReference, or yReference in some tutorial, just know – they are deprecated. There are now anchorX and anchorY isntead, and they’re default values are 0.5, which can be changed with the call like:

display.setDefault("anchorX", 0)

Further info

Transforms and Anchors

Anchor Points in Graphics 2.0

Hide status and navigation bars on Android

Let me guess. You just want to hide every single bar and have a full-screen experience for your game, right? Well, it’s not like there’s an On/Off switch for that. It’s a bit more divergent. In order to hide a status bar on top, just use the following line:

display.setStatusBar(display.HiddenStatusBar)

What about navigation one, you have to set androidSystemUiVisibility property to either immersiveSticky or immersive. The first one will hide all bars and make them re-appear on user’s top to bottom swype gesture, and the second one will do the same, except the bars won’t hide automatically, but will stay visible. So, most probably, you will choose immersiveSticky. There is also lowProfile, but it only dims navigation bar icons. And, in order to reset the setting, there’s default value as well.

native.setProperty("androidSystemUiVisibility", "immersiveSticky")

Bad thing is, this line of code works for Android 4.4 (KitKat) and above only. This is because Android’s immersive full-screen mode was introduced quite recently. Good thing, it will not produce a crash if the version is lower, so you not have to worry about version checks, – Corona handles it for you 😉

To sum up, use two of the above lines. If Android’s version is less than 4.4, at least you will have a status bar hidden.

Further info

androidSystemUiVisibility property values

Using Immersive Full-Screen Mode in Android

Position objects dynamically

If you make an Android game, you know how painful items’s positioning can be. And what if you want some object to be always on the bottom, regardless of device’s screen size? Or in the center? On aligned to the left? Ideally, with a few px padding… It’s not like you can’t do this on your own, but why reinvent the wheel? Magnet library makes it easy!

The usage is very straightforward. Just pick one of methods and apply (optionally) X or Y padding. For example,

magnet:center(btnPlay, 0, 50)

positions btnPlay 50 px lower from screen’s center.

And that’s it for today! I also plan the second part of Tips and Tricks in Corona SDK, so if you have something in mind – just write a comment below and I will consider it 😉

Thanks for reading!

Like and share:

Published by

Tonia Tkachuk

I’m an Android Developer, writing code for living and for fun. Love beautiful apps with a clean code inside. Enjoy travelling and reading.